I had to move the Nested case statement to the inner sql and i'm getting an error that says: Column 'dbo.THIT_RATIO_DETL.STATUS_CD' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. If i remove this column: (CASE WHEN dbo.THIT_RATIO_DETL.STATUS_CD = 'B' or dbo.THIT_RATIO_DETL.STATUS_CD = 'K' Then Sum(CASE WHEN dbo.THIT_RATIO_DETL.DED_AGR_AM=0 THEN dbo.THIT_RATIO_DETL.DED_OCR_AM else dbo.THIT_RATIO_DETL.DED_AGR_AM end) ELSE 0 END)as test
it works fine, in my other column i have the same reference to the status cd, so I’m not sure why I have to put the status code in the group by just for this column and not the others… if i add the status cd to the group by statement it throws my results off, if anybody has any suggestions it's greatly appreciated...Full Query: SELECT F_DIVISION_NO, Bound, Primary_SIR, Test From ( Select DBO.THIT_RATIO_DETL.F_DIVISION_NO, Sum(CASE WHEN dbo.THIT_RATIO_DETL.STATUS_CD = "B" or dbo.THIT_RATIO_DETL.STATUS_CD = "K" Then 1 ELSE 0 END) as Bound, Sum(CASE WHEN dbo.THIT_RATIO_DETL.DED_AGR_AM=0 THEN dbo.THIT_RATIO_DETL.DED_OCR_AM else dbo.THIT_RATIO_DETL.DED_AGR_AM end) as Primary_SIR, (CASE WHEN dbo.THIT_RATIO_DETL.STATUS_CD = 'B' or dbo.THIT_RATIO_DETL.STATUS_CD = 'K' Then Sum(CASE WHEN dbo.THIT_RATIO_DETL.DED_AGR_AM=0 THEN dbo.THIT_RATIO_DETL.DED_OCR_AM else dbo.THIT_RATIO_DETL.DED_AGR_AM end) ELSE 0 END)as test FROM dbo.THIT_RATIO_DETL Group by F_
DIVISION_NO ) t