group by下同一列字符串链接函数及sql(sql server 2000下)[转]

本文详细解析了SQL中如何使用聚合函数进行数据处理,并通过实例展示了如何利用函数实现特定的数据分组和输出需求。从创建表、插入数据,到定义聚合函数和调用函数,再到最终结果展示,一步步深入浅出地阐述了SQL在数据处理领域的强大能力。

 create table tb(id int,txt varchar(100))  
  go  
  insert into tb  
  select 1,'aaa' union all  
  select 1,'bbb' union all  
  select 2,'ccc' union all  
  select 3,'ddd' union all  
  select 3,'eee' union all  
  select 3,'fff'  
   
  go  
  --写一个聚合函数:  
  create function dbo.fn_Merge(@id int)  
  returns varchar(8000)  
  as  
  begin  
  declare @r varchar(8000)  
  set @r=''  
  select @r=@r+';'+txt from tb where id=@id  
  return stuff(@r,1,1,'')  
  end  
  go  
   
  -- 调用函数  
  select id, dbo.fn_Merge(id) as txt from tb group by id  
   
  go  
  drop table tb  
  drop function fn_Merge
这条语句在2005是能执行出来的,可是2000里确不行,大家给看看,大概意思就是,我要得到 下面的结果
id txt  
  1 aaa  
  1 bbb  
  2 ccc  
  3 ddd  
  3 eee  
  3 fff  
   
   
  select id,***(txt,';') from tb group by id  
  结果:  
  1 aaa;bbb  
  2 ccc  
  3 ddd;eee;fff

 

转自:http://topic.csdn.net/u/20100520/15/176afe53-0a0c-40cd-86ac-c2a3a3b41130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值