答案 0 :(得分:1)
示例强>
Select Count=[C1],Pays=[P1],Count=[C2],Pays=[P2],Count=[C3],Pays=[P3],Count=[C4],Pays=[P4]
From (
Select B.*
From (
Select *,RN = Row_Number() over (Order By Pays)
From YourTable
) A
Cross Apply (values ('P'+cast(A.RN as varchar(25)),A.Pays)
,('C'+cast(A.RN as varchar(25)),cast(A.Count as varchar(50)))
) B (Item,Value)
) A
Pivot (max(value) For [Item] in ([C1],[P1],[C2],[P2],[C3],[P3],[C4],[P4]) ) p
<强>返回强>
如果有帮助,生成的SQL看起来像这样:
A