create table a (col int) insert a select 1 union select 2 union select 3 union select 4 union select 5 select col1=sum(case col when 1 then 1 end), col1=sum(case col when 2 then 2 end), col1=sum(case col when 3 then 3 end), col1=sum(case col when 4 then 4 end), col1=sum(case col when 5 then 5 end) from a
如果数据列很多的话 我看你最好在excel里把它转置后再导入好了,最简单不用想了
drop table A create table a (col int) insert a select 1 union select 2 union select 3 union select 4 union select 5 DECLARE @T_sql varchar(8000) set @T_sql='' select @T_sql=@T_sql + ' max(case when col=' + cast(col as varchar) + ' then col else 0 end) as ''' + cast(col as varchar) + ''',' from A set @T_sql='select' + left(@T_sql,len(@T_sql)-1) + ' from A' exec (@T_sql)
(col int)
insert a select 1
union select 2
union select 3
union select 4
union select 5
select col1=sum(case col when 1 then 1 end),
col1=sum(case col when 2 then 2 end),
col1=sum(case col when 3 then 3 end),
col1=sum(case col when 4 then 4 end),
col1=sum(case col when 5 then 5 end)
from a
create table a
(col int)
insert a select 1
union select 2
union select 3
union select 4
union select 5
DECLARE @T_sql varchar(8000)
set @T_sql=''
select @T_sql=@T_sql + ' max(case when col=' + cast(col as varchar) + ' then col else 0 end) as ''' + cast(col as varchar) + ''',' from A
set @T_sql='select' + left(@T_sql,len(@T_sql)-1) + ' from A'
exec (@T_sql)