I have a table called balance
which I wish to pivot, however it is quite difficult since the column names would be labelled 1,2,3 and balances would be sorted b
You can try this way
select *,concat('Price',RANK() OVER (PARTITION BY i.REFERENCE_NO ORDER BY i.TASK_ID
DESC)) AS Rank into #temp from [dbo].[Table_1] i
select REFERENCE_NO,Price1,Price2,Price3 from
(
select REFERENCE_NO,TASK_ID,Rank from #temp
) as cte
PIVOT (SUM(TASK_ID)
FOR rank IN (Price1,Price2,Price3 ))
as PVT