admno |
Names |
Class |
PayDates |
Amount |
19-07808 |
SSENYONGA JOTHAM ATWINE |
P6D |
23/02/2024 |
100,000 |
19-07808 |
SSENYONGA JOTHAM ATWINE |
P6D |
24/02/2024 |
90,000 |
19-07529 |
NAMBOOZE LYTON ANN |
P6D |
24/02/2024 |
20,000 |
20-02085 |
BAKASHABA ADRIAN |
P6D |
24/02/2024 |
20,000 |
19-07674 |
MIREMBE GILLIAN |
P6D |
24/02/2024 |
180,000 |
How can i display a summation of amounts by pivoting the paydates using Sql Server.
DECLARE @cols AS NVARCHAR(MAX)
DECLARE @query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(Dates) from Paytemp as t1
GROUP BY Dates ORDER BY Dates FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = ';WITH CTE AS(
select amount,Dates,Names, admno
from paytemp
)
SELECT SUM(amount) amount,Names,' + @cols + ' from CTE
pivot(max(admno) for Dates in (' + @cols + ')) p
GROUP BY Names,' + @cols + '
'
execute(@query);
Thanks in advance.