34,576
社区成员
发帖
与我相关
我的任务
分享
/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳
描述:将如下形式的数据按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
if object_id('tb') is not null drop table tb
create table tb (id int,qus varchar(50),uid int)
insert into tb
select 1,'问题1',1 union all
select 2,'问题2',1 union all
select 3,'问题3',1 union all
select 4,'问题4',2
if object_id('tc') is not null drop table tc
create table tc (id int,uname varchar(50),content varchar(100))
insert into tc
select 1,'user','aaa' union all
select 2,'u2','bbb'
--=============
declare @sql varchar(8000)
set @sql = 'select uid , uname '
select @sql = @sql + ' , max(case cn when ''' + cast(cn as varchar) + ''' then qus else '''' end)
[qus' + cast(cn as varchar) + ']'
from (select distinct cn from (select *,cn=row_number() over(partition by uid order by uid) from tb) as a) m
set @sql = @sql + ' from (select *,cn=row_number() over(partition by uid order by uid) from tb) as a
join tc as b on a.uid = b.id group by uid,uname order by uid'
select @sql
exec(@sql)
--->打印出的sql
select uid , uname ,
max(case cn when '1' then qus else '' end) [qus1] ,
max(case cn when '2' then qus else '' end) [qus2] ,
max(case cn when '3' then qus else '' end) [qus3]
from (select *,cn=row_number() over(partition by uid order by uid) from tb) as a
join tc as b on a.uid = b.id group by uid,uname order by uid
--=========结果
1 user 问题1 问题2 问题3
2 u2 问题4
if OBJECT_ID('表1') is not null
drop table 表1
if OBJECT_ID('表2')is not null
drop table 表2
if OBJECT_ID('c_f')is not null
drop function c_f
go
create table 表1 (id int,answer nvarchar(10),userid int)
insert into 表1 select 1,'问题1',1
union all select 2,'问题2',1
union all select 3,'问题3',1
create table 表2 (userid int,username nvarchar(10),content int)
insert into 表2 select 1,'user',3333
go
create function c_f (@userid int)
returns nvarchar(100)
as
begin
declare @str nvarchar(100)
set @str=''
select @str=@str + ' '+ answer from 表1 a
join 表2 b on a.userid=b.userid
where a.userid=@userid
return @str
end
go
select B.* ,列=dbo.c_f(B.userid) from 表1 a join 表2 B
on a.userid=B.userid
group by b.userid,b.username,b.content
if OBJECT_ID('表1') is not null
drop table 表1
if OBJECT_ID('表2')is not null
drop table 表2
if OBJECT_ID('c_f')is not null
drop function c_f
go
create table 表1 (id int,answer nvarchar(10),userid int)
insert into 表1 select 1,'问题1',1
union all select 2,'问题2',1
union all select 3,'问题3',1
create table 表2 (userid int,username nvarchar(10),content int)
insert into 表2 select 1,'user',3333
go
create function c_f (@userid int)
returns nvarchar(100)
as
begin
declare @str nvarchar(100)
set @str=''
select @str=@str + ' '+ answer from 表1 a
--join 表2 b on a.userid=b.userid
where a.userid=@userid
--group by a.userid
return stuff(@str,1,1,'')
end
go
select top 1 B.* ,列=dbo.c_f(A..id) from 表1 a join 表2 B
on a.userid=B.userid
userid username content 列
----------- ---------- ----------- ----------------------------------------------------------------------------------------------------
1 user 3333 问题1 问题2 问题3
(1 行受影响)
问题描述:
无论是在sql 2000,还是在 sql 2005 中,都没有提供字符串的聚合函数,
所以,当我们在处理下列要求时,会比较麻烦:
有表tb, 如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即, group by id, 求 value 的和(字符串相加)
1. 旧的解决方法
-- 1. 创建处理函数
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value
FROM tb
WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数
SELECt id, values=dbo.f_str(id)
FROM tb
GROUP BY id
-- 2. 新的解决方法(适用于2005及以后版本)
-- 示例数据
DECLARE @t TABLE(id int, value varchar(10))
INSERT @t SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'
-- 查询处理
SELECT *
FROM(
SELECT DISTINCT
id
FROM @t
)A
OUTER APPLY(
SELECT
[values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM @t N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N
/*--结果
id values
----------- ----------------
1 aa,bb
2 aaa,bbb,ccc
(2 行受影响)
--*/
CSDN 社区帖子地址
附: 合并与分拆的CLR, sql2005的示例中有:
在安装sql 2005的示例后,默认安装目录为
drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\StringUtilities中