首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-15 
求一简单sql语句。。。。。。。。表a1a2a3结果a123[解决办法]declare @tb table (name varchar(10),id int)insert

求一简单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 @s=@s+ ',sum(case id when ' ' '+rtrim(id)+ ' ' ' then id end) 'from tb group by id

select @s=@s+ ' from tb group by name '
exec(@s)

drop table tb

热点排行