Home » SQL & PL/SQL » SQL & PL/SQL » sql
sql [message #651042] Tue, 10 May 2016 08:08 Go to next message
desmond30
Messages: 41
Registered: November 2009
Member


create table gtest(x number, y date);

insert into gtest values (1,sysdate);

insert into gtest values (2,sysdate);

insert into gtest values (2,null);

insert into gtest values (3,null);

insert into gtest values (3,null);

commit;


I am working on a query that will give only the values of x that has y as null

the result should give me only 3, though there are nulls for 2, since theres a date populated for 2, we should not see it in the result
Re: sql [message #651043 is a reply to message #651042] Tue, 10 May 2016 08:13 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
What code have you tried so far?
Re: sql [message #651044 is a reply to message #651042] Tue, 10 May 2016 08:14 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SELECT X FROM GTEST WHERE Y IS NULL;
Re: sql [message #651045 is a reply to message #651044] Tue, 10 May 2016 08:15 Go to previous messageGo to next message
desmond30
Messages: 41
Registered: November 2009
Member
select x from gtest where y is null
minus
select distinct x from gtest where y is not null
Re: sql [message #651049 is a reply to message #651045] Tue, 10 May 2016 08:25 Go to previous messageGo to next message
cookiemonster
Messages: 13922
Registered: September 2008
Location: Rainy Manchester
Senior Member
You don't need the distinct.
Re: sql [message #651053 is a reply to message #651049] Tue, 10 May 2016 10:19 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
A minus (and union) query automatically removes duplicates
Re: sql [message #651055 is a reply to message #651045] Tue, 10 May 2016 10:51 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Besides unnecessary DISTINCT it is less efficient than using analytic count:

with t as (
           select  x,
                   count(y) over(partition by x) cnt
             from  gtest
          )
select  x
  from  t
  where cnt = 0
/


The above will scan table gtest once comparing to twice when using minus.

SY.
Re: sql [message #651057 is a reply to message #651042] Tue, 10 May 2016 11:00 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
And another solution:
orclz>
orclz> select distinct x from gtest where x not in (select x from gtest where y is not null);

         X
----------
         3

orclz>
On my 12.1.0.2 DB, the cost is less than your solution but more than SY's.
Re: sql [message #651061 is a reply to message #651057] Tue, 10 May 2016 11:31 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Your solution isn't equivalent to OP's (or mine) solutions. It will not work if X is null.

SY.
Previous Topic: search for text in views
Next Topic: help in query
Goto Forum:
  


Current Time: Thu May 09 08:21:57 CDT 2024