在MSSQL中将表Table2Dimension转换为Table DesiredTable的正确语法是什么?
答案 0 :(得分:1)
由于您使用的是SQL Server,因此可以使用PIVOT函数获取结果:
select [row], [1], [2], [3]
from
(
select [row], col, value
from Table2Dimension
) d
pivot
(
max(value)
for col in ([1], [2], [3])
) piv;
见SQL Fiddle with Demo。这给出了结果:
| ROW | 1 | 2 | 3 |
|-----|---|---|---|
| 1 | 1 | 2 | 3 |
| 2 | 4 | 5 | 6 |
| 3 | 7 | 8 | 9 |
答案 1 :(得分:0)
select row,
sum(case when col = 1 then value end) as [1],
sum(case when col = 2 then value end) as [2],
sum(case when col = 3 then value end) as [3]
from your_table
group by row