日期:2014-05-17 浏览次数:20407 次
DECLARE @s NVARCHAR(4000)
SET @s = ''
SELECT @s = @s + ',' + QUOTENAME([sid]) + '=max(case when [sid]='
+ QUOTENAME([sid], '''') + ' then [result] else 0.0 end)'
FROM tb
GROUP BY [sid]
SELECT @s = SUBSTRING(@s, 2, LEN(@s))
EXEC('select '+@s+' from tb ')
declare @table table
(
id int,
sid nvarchar(5),
result nvarchar(10)
)
insert into @table(id,sid,result)
select 1 ,'001' ,'80.0' union all
select 2 ,'001' ,'90.0' union all
select 3 ,'001' ,'80.0' union all
select 4 ,'002' ,'56.0' union all
select 5 ,'002' ,'69.0' union all
select 6 ,'002' ,'89.0'
select sid=sid+' '+(select result+' ' from @table where sid=tab.sid for xml path(''))
from
(
select sid from @table group by sid
)tab
declare @table table
(
id int,
sid nvarchar(5),
result nvarchar(10)
)
insert into @table(id,sid,result)
select 1 ,'001' ,'80.0' union all
select 2 ,'001' ,'90.0' union all
select 3 ,'001' ,'80.0' union all
select 4 ,'002' ,'56.0' union all
select 5 ,'002' ,'69.0' union all
select 6 ,'002' ,'89.0'
select sid=sid+' '+(select result+' ' from @table where sid=tab.sid for xml path(''))
from
(
select sid from @table group by sid
)tab
/*
sid
----------------------------------------------------------------------------------------------------------------
001 80.0 90.0 80.0
002 56.0 69.0 89.0
*/