Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Subselect removing nulls from main query

Author  Topic 

ITTrucker
Yak Posting Veteran

64 Posts

Posted - 2012-05-14 : 11:26:52
Nulls in main data set are being excluded by a where clause subquery that doesn't contain any nulls. (Simplified query below)

I have a query pulling a bunch of invoice data that I need to exclude some certain records from. The only way to get the records that need to be excluded is from a subselect of another table. The subselect does not include nulls, it returns numbers.

The problem is in the main invoice tables, we have miscellaneous invoices that have a NULL mov_number and the subquery is removing those records from the data.

There are a bunch of articles about dealing with NULL in the subquery, but this is the opposite, I want to keep the NULLs in the main query using a subquery with no NULLs.

select
'BILL TO' = C.CMP_NAME,
'ORDER#' = H.ord_hdrnumber,
'MOVEMENT' = H.mov_number,
'INVOICE#' = CONVERT(VARCHAR(12),H.ivh_invoicenumber),
'TOTAL' = SUM(d.ivd_charge)

from invoiceheader H INNER JOIN invoicedetail D ON H.ivh_hdrnumber = D.ivh_hdrnumber INNER JOIN COMPANY C ON H.IVH_BILLTO = C.CMP_ID

WHERE
(H.mov_number NOT IN (select DISTINCT evt_mov_number from event where evt_trailer2 <> 'UNKNOWN' and evt_enddate >= '20110101'))

GROUP BY
C.CMP_NAME, H.ord_hdrnumber, H.ivh_invoicenumber,H.mov_number

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-05-14 : 11:58:39
How about adding an or-statement:


select 'BILL TO' as C.CMP_NAME
,'ORDER#' as H.ord_hdrnumber
,'MOVEMENT' as H.mov_number
,'INVOICE#' as CONVERT(VARCHAR(12),H.ivh_invoicenumber)
,'TOTAL' as SUM(d.ivd_charge)
from invoiceheader H
INNER JOIN invoicedetail D
ON H.ivh_hdrnumber = D.ivh_hdrnumber
INNER JOIN COMPANY C
ON H.IVH_BILLTO = C.CMP_ID
WHERE H.mov_number NOT IN (select DISTINCT evt_mov_number
from event
where evt_trailer2 <> 'UNKNOWN'
and evt_enddate >= '20110101'
)
or H.mov_number is null
Go to Top of Page

ITTrucker
Yak Posting Veteran

64 Posts

Posted - 2012-05-14 : 12:39:27
My fault for simplifying the query too much. There are a bunch of where clauses already in the query and I want to evaluate those as well. I don't want to keep all the NULL values, just the ones that fit the rest of the where clauses.

But I think I figured it out. I have other subselects that are not eliminating the NULL values, so instead of evaluating on the H.mov_number which has NULL values, I added an additional join in my subselect so I was evaluating on the H.ord_hdrnumber which is 0 for null values, but returns the data I'm looking for.


WHERE
H.ord_hdrnumber NOT in (select DISTINCT o.ord_hdrnumber from event e inner join orderheader o on e.evt_mov_number = o.mov_number where evt_trailer2 <> 'UNKNOWN' and evt_enddate >= '20110101')


Thanks for your help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-14 : 22:18:43
quote:
Originally posted by ITTrucker

My fault for simplifying the query too much. There are a bunch of where clauses already in the query and I want to evaluate those as well. I don't want to keep all the NULL values, just the ones that fit the rest of the where clauses.

But I think I figured it out. I have other subselects that are not eliminating the NULL values, so instead of evaluating on the H.mov_number which has NULL values, I added an additional join in my subselect so I was evaluating on the H.ord_hdrnumber which is 0 for null values, but returns the data I'm looking for.


WHERE
H.ord_hdrnumber NOT in (select DISTINCT o.ord_hdrnumber from event e inner join orderheader o on e.evt_mov_number = o.mov_number where evt_trailer2 <> 'UNKNOWN' and evt_enddate >= '20110101')


Thanks for your help.


if you want to consider NULL values also in column used for comparison you need to either use LEFT JOIN or having an additional condition to check if column value is NULL separated by OR along with IN condition check

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -