问题描述: 无论是在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中
create table Y ( id int, name nvarchar(20) ) insert into Y select 1 , 'a' union all select 1 , 'b' union all select 1 , 'c' union all select 2 , 'd' union all select 2 , 'e' union all select 3 , 'f' create function ky(@id as int) returns nvarchar(30) as begin declare @s nvarchar(30) set @s='' select @s=Isnull(@s+name+',','') from Y where id=@id return substring(@s,1,len(@s)-1) end goselect id,name=dbo.ky(id),MAX(GetDATE()) from Y group by id
没试过这样的总是。用数据窗口来实现不行吗?非要用SQL吗?
---测试数据--- if object_id('[tb]') is not null drop table [tb] go create table [tb]([id] int,[name] varchar(1)) insert [tb] select 1,'a' union all select 1,'b' union all select 1,'c' union all select 2,'d' union all select 2,'e' union all select 3,'f'---创建字符连接函数--- CREATE FUNCTION dbo.f_str(@id varchar(20)) RETURNS varchar(8000) AS BEGIN DECLARE @s varchar(8000) SET @s = '' SELECT @s = @s + '、'+ name FROM tb where id=@id RETURN STUFF(@s, 1, 1, '') END
---查询--- select distinct id,dbo.f_str(id) as name from [tb]---结果--- id name ----------- -------------------- 1 a、b、c 2 d、e 3 f(所影响的行数为 3 行)
create table Y ( id int, name nvarchar(20) ) insert into Y select 1 , 'a' union all select 1 , 'b' union all select 1 , 'c' union all select 2 , 'd' union all select 2 , 'e' union all select 3 , 'f' create function ky(@id as int) returns nvarchar(30) as begin declare @s nvarchar(30) set @s='' select @s=Isnull(@s+name+','、'') from Y where id=@id return substring(@s,1,len(@s)-1) end goselect id,name=dbo.ky(id) from Y group by id
create table Y ( id int, name nvarchar(20) ) insert into Y select 1 , 'a' union all select 1 , 'b' union all select 1 , 'c' union all select 2 , 'd' union all select 2 , 'e' union all select 3 , 'f' create function ky(@id as int) returns nvarchar(30) as begin declare @s nvarchar(30) set @s='' select @s=Isnull(@s+name+',','') from Y where id=@id return substring(@s,1,len(@s)-1) end goselect id,name=dbo.ky(id) from Y group by id
create table tt(id int,name char) insert tt select 1,'a' union all select 1,'b' union all select 1,'c' union all select 2,'d' union all select 2,'e' union all select 2,'f' -- create function fn_str(@id int) returns varchar(8000) begin declare @str varchar(8000) select @str=isnull(@str+',','')+name from tt where id=@id return @str end -- select id,dbo.fn_str(id)as name from tt group by id
if object_id('[tb]') is not null drop table [tb] go create table [tb]([id] int,[name] varchar(1)) insert [tb] select 1,'a' union all select 1,'b' union all select 1,'c' union all select 2,'d' union all select 2,'e' union all select 3,'f'---创建字符连接函数--- CREATE FUNCTION dbo.f_str(@id varchar(20)) RETURNS varchar(8000) AS BEGIN DECLARE @s varchar(8000) SET @s = '' SELECT @s = @s + '、'+ name FROM tb where id=@id RETURN STUFF(@s, 1, 1, '') END
无论是在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中
(
id int,
name nvarchar(20)
)
insert into Y
select 1 , 'a' union all
select 1 , 'b' union all
select 1 , 'c' union all
select 2 , 'd' union all
select 2 , 'e' union all
select 3 , 'f'
create function ky(@id as int)
returns nvarchar(30)
as
begin
declare @s nvarchar(30)
set @s=''
select @s=Isnull(@s+name+',','') from Y where id=@id
return substring(@s,1,len(@s)-1)
end
goselect id,name=dbo.ky(id),MAX(GetDATE()) from Y group by id
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(1))
insert [tb]
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'d' union all
select 2,'e' union all
select 3,'f'---创建字符连接函数---
CREATE FUNCTION dbo.f_str(@id varchar(20))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @s varchar(8000)
SET @s = ''
SELECT @s = @s + '、'+ name
FROM tb where id=@id
RETURN STUFF(@s, 1, 1, '')
END
---查询---
select distinct id,dbo.f_str(id) as name from [tb]---结果---
id name
----------- --------------------
1 a、b、c
2 d、e
3 f(所影响的行数为 3 行)
(
id int,
name nvarchar(20)
)
insert into Y
select 1 , 'a' union all
select 1 , 'b' union all
select 1 , 'c' union all
select 2 , 'd' union all
select 2 , 'e' union all
select 3 , 'f'
create function ky(@id as int)
returns nvarchar(30)
as
begin
declare @s nvarchar(30)
set @s=''
select @s=Isnull(@s+name+','、'') from Y where id=@id
return substring(@s,1,len(@s)-1)
end
goselect id,name=dbo.ky(id) from Y group by id
(
id int,
name nvarchar(20)
)
insert into Y
select 1 , 'a' union all
select 1 , 'b' union all
select 1 , 'c' union all
select 2 , 'd' union all
select 2 , 'e' union all
select 3 , 'f'
create function ky(@id as int)
returns nvarchar(30)
as
begin
declare @s nvarchar(30)
set @s=''
select @s=Isnull(@s+name+',','') from Y where id=@id
return substring(@s,1,len(@s)-1)
end
goselect id,name=dbo.ky(id) from Y group by id
create table tt(id int,name char)
insert tt select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'d' union all
select 2,'e' union all
select 2,'f'
--
create function fn_str(@id int)
returns varchar(8000)
begin
declare @str varchar(8000)
select @str=isnull(@str+',','')+name from tt where id=@id
return @str
end
--
select id,dbo.fn_str(id)as name from tt group by id
go
create table [tb]([id] int,[name] varchar(1))
insert [tb]
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'d' union all
select 2,'e' union all
select 3,'f'---创建字符连接函数---
CREATE FUNCTION dbo.f_str(@id varchar(20))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @s varchar(8000)
SET @s = ''
SELECT @s = @s + '、'+ name
FROM tb where id=@id
RETURN STUFF(@s, 1, 1, '')
END