Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Puzzling SQL problem with nulls and dates
Columns A, B and C are all date types
In the queries below, query 1) counted where any of the three columns
were not null.
Queries 2,3,4 counted where any individual column A,B,C were null.
It seems that then subtracting the duplications of non-nullness
betweeen columns
from the sum of queries 2,3,4 should get the same result as query 1).
But, no. Why not? The same numbers are gotten if the distinct is
dropped or if
'distinct id' is replaced by *. These were spooled from a table at
work and the
column and snapshot names have been changed for clarity.
1696 A is not null Query 2 344 or B is not null Query 3 1404 or C is not null Query 4
- 42 if there is a duplication between A and B but not C
-261 if there is a duplication between A and C but not B
- 85 if there is a duplication between B and C but not A
- 30 if there is a 'triplication' between A, B, and C
This does not match Query 1) immediately below.
What gives? Thanks in advance.
Al
alchase_at_airmail.net
SQL>-- 1) count if any column is not nullSQL> SELECT count(distinct id)
COUNT(DISTINCTID)
2996
SQL>-- 2) count if Column_A is not nullSQL>
COUNT(DISTINCTID)
1696
SQL>-- 3) count if Column_B is not nullSQL>
COUNT(DISTINCTID)
344
SQL>-- 4) count if Column_C is not nullSQL>
COUNT(DISTINCTID)
1404
SQL>-- 5) count if Column A and B are not null butColumn C is
COUNT(DISTINCTID)
42
SQL>-- 6) count if Column A and C are not null butColumn B is
COUNT(DISTINCTID)
261
SQL>-- 7) count if Column B and C are not null butColumn A is
COUNT(DISTINCTID)
85
SQL>-- 8) count if Column A, B and C are all notnull
COUNT(DISTINCTID)
30
SQL>
SQL> spool off;
Received on Thu Jan 30 1997 - 00:00:00 CST
![]() |
![]() |