I have the table below and would like to transpose this in SQL, I have tried using sql PIVOT but I am getting nowhere. 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
