简体   繁体   中英

pivoting result in SQL Server

I have the following SQL:

SELECT
    PhaseId,    
    COUNT(JoinId)
FROM Joins
GROUP BY
    PhaseId

OUTPUT:

1   143
2   65
3   86

I usually pivot the result by using the case technique for each column, but now I'm trying to use the PIVOT statement unsuccessfully. Can anyone point me in the right direction?

I feel like there are many examples out there, but PIVOT is hard to wrap your head around, so:

SELECT *
FROM   
 ( SELECT PhaseId,JoinID
   FROM YourTable
  ) AS T1
PIVOT (COUNT(JoinID) FOR PhaseId IN ([1],[2],[3])) AS T2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM