Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

riga convertita in colonna in sql 2008

Puoi utilizzare lo stesso approccio di rank() e quindi utilizzare la nuova funzione PIVOT come segue:

with    cusCte as(
select  cusid,cusph1,RANK() over (partition by cusid order by cusph1) r
from    #cusphone)

SELECT cusid, [1] AS C1, [2] AS C2, [3] AS C3
FROM 
(SELECT cusid,cusph1,r
FROM cusCte) p
PIVOT
(
MIN (cusph1)
FOR r IN
( [1], [2], [3] )
) AS pvt;