0

我有这个数据:

name      | coulmnnuber
Newyork   | 1
washington| 2
denmark   | 3
Holand    | 4

数据应如下所示:

1           2           3          4
New york    Washington  denmark    Holand
4

1 回答 1

4

您可以使用带有 CASE 表达式的聚合函数将数据行转换为列:

select 
  max(case when coulmnnuber = 1 then name end) [1],
  max(case when coulmnnuber = 2 then name end) [2],
  max(case when coulmnnuber = 3 then name end) [3],
  max(case when coulmnnuber = 4 then name end) [4]            
from yourtable;

请参阅带有演示的 SQL Fiddle

或者您可以使用 PIVOT 功能:

select [1], [2], [3], [4]
from yourtable
pivot
(
  max(name)
  for coulmnnuber in ([1], [2], [3], [4])
) piv;

请参阅带有演示的 SQL Fiddle

于 2013-08-16T16:07:36.083 回答