在 MSSQL 中从 Table Table2Dimension 转换为 Table DesiredTable 的正确语法是什么?
问问题
44 次
2 回答
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 |
于 2013-11-10T21:39:24.450 回答
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
于 2013-11-10T16:59:46.553 回答