当前位置: 代码迷 >> Sql Server >> 求一简单sql语句。该如何处理
  详细解决方案

求一简单sql语句。该如何处理

热度:39   发布时间:2016-04-27 18:58:20.0
求一简单sql语句。。。。。。。。

a   1
a   2
a   3
结果
a   1   2   3

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

declare @tb table (name varchar(10),id int)
insert into @tb
select 'a ',1
union select 'a ',2
union select 'a ',3

select name,
sum(case id when 1 then id end),
sum(case id when 2 then id end),
sum(case id when 3 then id end)
from @tb group by name
------解决方案--------------------
create table tb(name varchar(10),id int)
insert into tb
select 'a ',1
union select 'a ',2
union select 'a ',3


declare @s varchar(200)
set @s= 'select name '
select @[email protected]+ ',sum(case id when ' ' '+rtrim(id)+ ' ' ' then id end) 'from tb group by id

select @[email protected]+ ' from tb group by name '
exec(@s)

drop table tb
  相关解决方案