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.
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
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