lolol
lolol

Reputation: 4390

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?

Upvotes: 0

Views: 50

Answers (1)

Hart CO
Hart CO

Reputation: 34784

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

Upvotes: 2

Related Questions