Hi @SQL
Try this query:
SELECT Grp_Nbr
,AUD
,SUM(CASE WHEN (AUD = 'SUB' AND Rln_Cd = 1) OR (AUD = 'SSB' AND Rln_Cd IN(1,2)) OR (AUD = 'MEM' AND Rln_Cd IN(1,2,3,4,5))THEN Total END)AS Total
FROM @TestTbl
Group by Grp_Nbr, AUD
ORDER BY Grp_Nbr
Also, you could use UNION like this:
SELECT Grp_Nbr,AUD,SUM(Total)AS Total FROM @TestTbl
WHERE AUD = 'SUB' AND Rln_Cd = 1
Group by Grp_Nbr, AUD
UNION
SELECT Grp_Nbr,AUD,SUM(Total)AS Total FROM @TestTbl
WHERE AUD = 'SSB' AND Rln_Cd IN(1,2)
Group by Grp_Nbr, AUD
UNION
SELECT Grp_Nbr,AUD,SUM(Total)AS Total FROM @TestTbl
WHERE AUD = 'MEM' AND Rln_Cd IN(1,2,3,4,5)
Group by Grp_Nbr, AUD
ORDER BY Grp_Nbr
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.