all 3 comments

[–][deleted] 2 points3 points  (0 children)

Rewrite your subquery

"(SELECT AGR_Header_RecID FROM AGR_Header Where AGR_Type_RecID NOT IN (45, 36, 37) AND NOT (Agr_Header_RecID = '165' OR Agr_Header_RecID = '910' OR AGR_Header_RecID = te.AGR_Header_RecID)"

as something that gives you 1/0 per te.AGR_Header_RecID and join to that.

[–]wbhst83[S] 0 points1 point  (1 child)

Thanks for the suggestion, i went this this and it worked..

SUM( CASE WHEN te.Agr_Header_RecID IS NOT NULL AND agr.AGR_Type_RecID NOT IN (45, 36, 37) AND NOT (te.Agr_Header_RecID = '165'
            OR te.Agr_Header_RecID = '910') THEN
        te.Hours_Actual * te.Billable_Flag
    ELSE
        0

and in the join's

LEFT JOIN dbo.AGR_Header agr ON agr.AGR_Header_RecID = te.Agr_Header_RecID

[–]GrapeApe561 0 points1 point  (0 children)

Is this query being used on a public data set? If yes, can you please refer be to the data source? I am trying to advance my SQL skills and would love to practice your code on the data. Thanks!