62,046
社区成员
发帖
与我相关
我的任务
分享
/*
描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id value
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
create function dbo.f_str(@id int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return @str
end
go
--调用函数
select id , value = dbo.f_str(id) from tb group by id
drop function dbo.f_str
drop table tb
--2、sql2005中的方法
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id
drop table tb
--3、使用游标合并数据
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursor
select * from @t
drop table tb
create table t_fee(用户名称 varchar(20),月份 varchar(20),欠费金额 int)
insert t_fee select '张三','200901',100
union all select '张三','200902',100
union all select '张三','200903',100
union all select '张三','200904',100
union all select '李四','200901',100
union all select '李四','200902',100
go
select * from t_fee
select t.用户名称, sum(case when t.欠费金额>0 then 1 else 0 end)[欠费月份数量],sum(t.欠费金额)[欠费金额] from t_fee t group by t.用户名称;
用户名称 月份 欠费金额
-------------------- -------------------- -----------
张三 200901 100
张三 200902 100
张三 200903 100
张三 200904 100
李四 200901 100
李四 200902 100
(所影响的行数为 6 行)
用户名称 欠费月份数量 欠费金额
-------------------- ----------- -----------
李四 2 200
张三 4 400
(所影响的行数为 2 行)
--> Test Data: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] ([用户名称] varchar(4),[月份] int,[欠费金额] int)
insert into [tb]
select '张三',200901,100 union all
select '张三',200902,100 union all
select '张三',200903,100 union all
select '张三',200904,100 union all
select '李四',200901,100 union all
select '李四',200902,100
--select * from [tb]
go
--Code
create function f_str(@key varchar(50))
returns varchar(100)
as
begin
declare @sql varchar(100)
select @sql=isnull(@sql+',','')+ltrim(月份) from tb where 用户名称=@key
return @sql
end
go
select 用户名称,
dbo.f_str(用户名称) as 欠费月份,
count(1) as 欠费月份数量,
sum(欠费金额) as 欠费金额
from tb
group by 用户名称
order by 用户名称 desc
--Drop
drop table [tb]
drop function f_str
--Result
/*
用户名称 欠费月份 欠费月份数量 欠费金额
---- ---------------------------------------------------------------------------------------------------- ----------- -----------
张三 200901,200902,200903,200904 4 400
李四 200901,200902 2 200
*/
select 用户名称,wmsys.wm_concat(月份) as 欠费月份,count(*) as 欠费月份数量,sum(欠费金额) as 欠费金额
from t_fee
group by 用户名称