简体   繁体   English

在SQL Server中转动结果

[英]pivoting result in SQL Server

I have the following SQL: 我有以下SQL:

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

OUTPUT: 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. 我通常使用每个列的大小写技术来转动结果,但现在我试图使用PIVOT语句失败。 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: 我觉得那里有很多例子,但是PIVOT很难包裹,所以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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