[英]Transpose SQL columns to rows
I have the table below and would like to transpose this in SQL, I have tried using sql PIVOT but I am getting nowhere. 我有下表,并希望在SQL中转置它,我已经尝试使用SQL PIVOT但我无处可去。 Any help is much appreciated.
任何帮助深表感谢。
CID ActID ActType ActDate
1 10 Assessment 2017-08-09
1 11 StartOfPOC 2017-11-01
1 22 POC1 2017-11-03
1 22 POC2 2017-11-03
2 44 Report 2017-11-03
2 44 Planning 2017-11-03
3 66 Assessment 2017-11-06
3 66 POC1 2017-11-06
3 77 EndOfPOC 2017-11-06
I would like to transpose this table to the below 我想将此表转置到下面
CID ActType1 ActDate1 ActType2 ActDate2 ActType3 ActDate3 ActType4 ActDate4 ActType4 ActDate4
1 Assessment 2017-08-09 StartOfPOC 2017-11-01 POC1 2017-11-03 POC2 2017-11-03
2 POC1 2017-11-03 Planning 2017-11-03
3 Assessment 2017-11-06 POC1 2017-11-06 EndOfPOC 2017-11-06
Below is what i have but want to improve from here. 以下是我的想法,但希望从这里改进。
SELECT * FROM (
Select
CID
,ActID
,ActType
,ActDate
from #tbl r
where r.ActType in ('Assessment','Start of POC','POC1','POC2','Report','Planning','EndOfPOC')
) x
PIVOT( MAX(ActDate)
FOR ActType IN (
[Assessment]
,[Start of POC]
,[POC1]
,[POC2]
,[Reporting]
,[Planning]
,[End of POC]
)
) p
Thanks 谢谢
Rather than PIVOT, I would use a Conditional Aggregation in concert with Row_Number() 我会使用与Row_Number()一致的条件聚合而不是PIVOT
Example 例
Select CID
,AcctType1 = max(case when RN = 1 then ActType end)
,AcctDate1 = max(case when RN = 1 then ActDate end)
,AcctType2 = max(case when RN = 2 then ActType end)
,AcctDate2 = max(case when RN = 2 then ActDate end)
,AcctType3 = max(case when RN = 3 then ActType end)
,AcctDate3 = max(case when RN = 3 then ActDate end)
,AcctType4 = max(case when RN = 4 then ActType end)
,AcctDate4 = max(case when RN = 4 then ActDate end)
From (
Select *
,RN= Row_Number() over (Partition By CID Order by ActDate)
From YourTable
) A
Group By CID
Returns 返回
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.