A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
Please refer to:
;WITH cte
as(SELECT Level_Four_Name AS Customer,'Water' AS [Description]
,ti.Qty,ti.Rate,ti.Pending,Total=ti.QTY * ti.Rate
FROM #tbl_Account_L_Four ta
JOIN #tbl_Invoice ti
ON ta.Level_Four_ID=ti.Level_Four_ID
WHERE ta.Level_Four_ID=1001)
SELECT Customer,[Description],CAST(Qty as varchar)Qty,CAST(Rate as varchar) Rate,Total
FROM cte
UNION ALL
SELECT '','','','Net Total',Sum(Total) FROM cte
UNION ALL
SELECT '','','','Pending',(SELECT Pending FROM cte WHERE Pending<>0)
UNION ALL
SELECT '','','','Grand Total', Sum(Total)+(SELECT Pending FROM cte WHERE Pending<>0) FROM cte
Output:
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.