我有一张看起来像这样的表
Form Name Quoted Issued total
a 5251 967 100000
b 145 34 9779
c 2190 545 420000
我需要它看起来像这样。
a b c
Quoted 5251 145 2190
Issued 967 34 545
Total 10000 9779 420000
如何旋转表格?
答案 0 :(得分:0)
我确信有更好的解决方案,这很难看,但它确实起作用了:
SELECT *
FROM
(
SELECT FormName, Quoted
FROM mytable
) data
PIVOT
(
Max(Quoted) For FormName IN ([a], [b], [c])
) as DataPivot
UNION
SELECT *
FROM
(
SELECT FormName, Issued
FROM mytable
) data
PIVOT
(
Max(Issued) For FormName IN ([a], [b], [c])
) as DataPivot
UNION
SELECT *
FROM
(
SELECT FormName, Total
FROM mytable
) data
PIVOT
(
Max(Total) For FormName IN ([a], [b], [c])
) as DataPivot
我建议对PIVOT进行一些研究,它可以做很多事情。如果您需要动态创建此类查询,那么SO上有很多答案。