dbTalk Databases Forums  

NULL values in IN condition

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss NULL values in IN condition in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default NULL values in IN condition - 12-11-2008 , 10:54 AM






I want to obtain all the possible values of a VARCHAR2(1) column named
FOO, sort them (either ASC or DESC) and finally fetch to two top-most
rows. NULL values must be listed on top (ASC) or bottom (DESC). So I've
written these queries:

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values. Something like
FOO IN (NULL, 'A', 'B') is FALSE even when FOO is null; apparently,
because FOO=NULL is not TRUE. But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2", so...

WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.


Can you think of any trick to overcome this?


The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
SELECT NVL(FOO, 'xxxxxxxxx') FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--

Reply With Quote
  #2  
Old   
Peter Nilsson
 
Posts: n/a

Default Re: NULL values in IN condition - 12-11-2008 , 07:53 PM






"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:
Quote:
I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

* * * * WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
* * * * SELECT NVL(FOO, 'xxxxxxxxx') FROM (
* * * * * * * * SELECT DISTINCT FOO
* * * * * * * * FROM TABLE
* * * * * * * * ORDER BY FOO NULLS FIRST
* * * * ) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?
with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter


Reply With Quote
  #3  
Old   
Peter Nilsson
 
Posts: n/a

Default Re: NULL values in IN condition - 12-11-2008 , 07:53 PM



"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:
Quote:
I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

* * * * WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
* * * * SELECT NVL(FOO, 'xxxxxxxxx') FROM (
* * * * * * * * SELECT DISTINCT FOO
* * * * * * * * FROM TABLE
* * * * * * * * ORDER BY FOO NULLS FIRST
* * * * ) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?
with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter


Reply With Quote
  #4  
Old   
Peter Nilsson
 
Posts: n/a

Default Re: NULL values in IN condition - 12-11-2008 , 07:53 PM



"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:
Quote:
I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

* * * * WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
* * * * SELECT NVL(FOO, 'xxxxxxxxx') FROM (
* * * * * * * * SELECT DISTINCT FOO
* * * * * * * * FROM TABLE
* * * * * * * * ORDER BY FOO NULLS FIRST
* * * * ) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?
with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter


Reply With Quote
  #5  
Old   
Peter Nilsson
 
Posts: n/a

Default Re: NULL values in IN condition - 12-11-2008 , 07:53 PM



"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:
Quote:
I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
* * * * SELECT DISTINCT FOO
* * * * FROM TABLE
* * * * ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

* * * * WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
* * * * SELECT NVL(FOO, 'xxxxxxxxx') FROM (
* * * * * * * * SELECT DISTINCT FOO
* * * * * * * * FROM TABLE
* * * * * * * * ORDER BY FOO NULLS FIRST
* * * * ) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?
with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter


Reply With Quote
  #6  
Old   
Carlos
 
Posts: n/a

Default Re: NULL values in IN condition - 12-12-2008 , 02:10 AM



On 12 dic, 02:53, Peter Nilsson <ai... (AT) acay (DOT) com.au> wrote:
Quote:
"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:



I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
SELECT NVL(FOO, 'xxxxxxxxx') FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?

with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter
Why on earth could someone define a column as varchar2(1)??

Cheers.

Carlos.


Reply With Quote
  #7  
Old   
Carlos
 
Posts: n/a

Default Re: NULL values in IN condition - 12-12-2008 , 02:10 AM



On 12 dic, 02:53, Peter Nilsson <ai... (AT) acay (DOT) com.au> wrote:
Quote:
"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:



I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
SELECT NVL(FOO, 'xxxxxxxxx') FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?

with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter
Why on earth could someone define a column as varchar2(1)??

Cheers.

Carlos.


Reply With Quote
  #8  
Old   
Carlos
 
Posts: n/a

Default Re: NULL values in IN condition - 12-12-2008 , 02:10 AM



On 12 dic, 02:53, Peter Nilsson <ai... (AT) acay (DOT) com.au> wrote:
Quote:
"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:



I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
SELECT NVL(FOO, 'xxxxxxxxx') FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?

with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter
Why on earth could someone define a column as varchar2(1)??

Cheers.

Carlos.


Reply With Quote
  #9  
Old   
Carlos
 
Posts: n/a

Default Re: NULL values in IN condition - 12-12-2008 , 02:10 AM



On 12 dic, 02:53, Peter Nilsson <ai... (AT) acay (DOT) com.au> wrote:
Quote:
"Álvaro G. Vicario" <alvaroNOSPAMTHA... (AT) demogracia (DOT) com> wrote:



I want to obtain all the possible values of a VARCHAR2(1)
column named FOO, sort them (either ASC or DESC) and finally
fetch to two top-most rows. NULL values must be listed on
top (ASC) or bottom (DESC). So I've written these queries:

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an
IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values.
Something like FOO IN (NULL, 'A', 'B') is FALSE even when
FOO is null; apparently, because FOO=NULL is not TRUE.
But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2",
so...

WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.

Can you think of any trick to overcome this?

The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
SELECT NVL(FOO, 'xxxxxxxxx') FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
)

It works but... It looks so ugly! :-?

with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter
Why on earth could someone define a column as varchar2(1)??

Cheers.

Carlos.


Reply With Quote
  #10  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: NULL values in IN condition - 12-12-2008 , 04:34 AM



Carlos escribió:
Quote:
Why on earth could someone define a column as varchar2(1)??
What's your suggestion to store a single character?



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.