Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to exclude null values sometimes 1

Status
Not open for further replies.

CrystalQB

Programmer
Jan 9, 2008
18
US
This is a weird situation...but I'm hoping someone can point me in the right direction to pull the data I need.

This dataset requires me to pull all values (DtlVal) from a table where a HeaderCode is in an array of values.
(i.e. HdrCode in ('400000','400100','400200', '400300',
'400400','400500','400550','400555','400560',
'400600','400700','400800'))
and also there needs to be a value in the Property ID. If the Property ID is null then they don't want the value.

BUT!!! Here's the problem...if the Hdr code IS ('400200','400500','400550','400555','400800') they DO want to see the value even if the property ID is null.

Here's what I started with when they realized they wanted to make this exception.


Code:
SELECT DISTINCT 
  SUM(CASE 
    WHEN HdrCode in ('400000','400100','400200')
    THEN DtlVal
  END)*-1 AS GasSales,
  SUM(CASE 
    WHEN HdrCode in ('400300','400400','400500','400550',
    '400555','400560')
    THEN DtlVal
  END)*-1 AS OilSales,
  SUM(CASE 
    WHEN HdrCode in ('400600','400700','400800')
    THEN DtlVal
  END)*-1 AS NGLSales,
FROM	dbo.glMasDtl GL
INNER JOIN
	dbo.fbMasHdr Acct
ON	DtlAcctHID = HdrHID
WHERE  	
     HdrCode in ('400000','400100','400200', '400300',
        '400400','400500','400550','400555','400560',
        '400600','400700','400800')
AND  GL.DtlPropHID <> 0
AND  GL.AcctDate BETWEEN (@EndDate-400) AND (@EndDate)
AND  YEAR(GL.AcctDate) IN ((YEAR(getdate())), (YEAR(getdate())-1))

With the CASE statements I'm confused how to pull this off.
Let me know if you need further clarification. (Which wouldn't surprise me a bit, as this is very confusing for me to write. Not sure I'm stating it correctly or clearly enough.)

Any help would be MUCH appreciated.

Thanks,
CrystalQB

 
Select ...
where HdrCode in(xxxx) and is not null
union
select ...
where HdrCode in(yyyy)
 
That was too easy...I can't believe I didn't think of that! Thank you so much. It worked like a champ!

Thanks a million!
CrystalQB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top