在Sql中的行到列[重复]

时间:2022-09-23 21:33:07

Possible Duplicate:
How do i transform rows into columns in sql server 2005

可能重复:如何在sql server 2005中将行转换为列

Table name sa

表名sa

Number
10
20
30
40

Expected Result

Number  Number  Number  Number
10       20     30      40

2 个解决方案

#1


0  

Try to this query

尝试这个查询

select number from sa
declare @s varchar(8000)
select  @s = COALESCE(@s + ',', '') + cast(number as varchar(10)) + ' as number'
from    sa
exec('select '+@s)

#2


0  

Select 
case when 10 then 10 
     when 20 then 20
     when 30 then 30
     when 40 then 40
     else
     ''
end
from tablenAme

#1


0  

Try to this query

尝试这个查询

select number from sa
declare @s varchar(8000)
select  @s = COALESCE(@s + ',', '') + cast(number as varchar(10)) + ' as number'
from    sa
exec('select '+@s)

#2


0  

Select 
case when 10 then 10 
     when 20 then 20
     when 30 then 30
     when 40 then 40
     else
     ''
end
from tablenAme