Please help this is what i have managed to come up with
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);
amount |
Names |
Jan 8 2024 10:14AM |
Jan 8 2024 11:02AM |
Jan 8 2024 3:18PM |
1620000 |
AINOMUGISHA ABIGAIL EMMANUELLA |
NULL |
NULL |
NULL |
1620000 |
KALUNGI SHAN |
NULL |
NULL |
NULL |
1620000 |
SERWANJJA UPENDO TAMERA |
NULL |
NULL |
NULL |
|
this is what i would want to display
Names |
Jan 8 2024 10:14AM |
Jan 8 2024 11:02AM |
Jan 8 2024 3:18PM |
AINOMUGISHA ABIGAIL EMMANUELLA |
NULL |
1620000 |
NULL |
KALUNGI SHAN |
1620000 |
NULL |
NULL |
SERWANJJA UPENDO TAMERA |
NULL |
NULL |
1620000 |
|