日期:2014-05-17  浏览次数:20351 次

sql server 语句报错, 各位大神来看看,,



------解决方案--------------------

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 @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 ')
 
------解决方案--------------------
引用:
引用:
引用:SQL code?



1234567891011121314151617181920

declare @table table(     id int,     sid nvarchar(5),     result nvarchar(10) ) insert into @ta……
只有一列了,他要的最少要把sid单独一列,result一列。这是你脚本的结果:


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 
*/