表A
代号 名称
1 a
1 b
1 c
2 a
2 a
2 d
3 d
3 c
3 e
... ...
想得到:
代号 名称
1 a,b,c
2 a,d
3 c,d,e
.. ...
请教各位大师,帮忙,十分感谢!!!
--参考
字符串汇总问题,在SQL Server 2000下受推荐的方式为:用户定义函数。具体处理方法及实现请参考:
---------------------------------------------------------------------------------------
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go
--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go
--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
------------------------------------------------------------------------
create function rowtocol(@id int)
returns varchar(1000)
as
begin
declare @str varchar(1000)
set @str = ''
select @str=@str+','+名称 from 表A where 代号 = @id
set @str = staff(@str,1,1,'')
return @str
end
select 代号,dbo.rowtocol(代号) from 表A group by 代号
---------------------------------------------------------------------------
create table 表A(代号 int,名称 varchar(20))
insert into 表A select 1,'a'
insert into 表A select 1,'b'
insert into 表A select 1,'c'
insert into 表A select 2,'a'
insert into 表A select 2,'a'
insert into 表A select 2,'d'
insert into 表A select 3,'d'
insert into 表A select 3,'c'
insert into 表A select 3,'e'
go
drop function rowtocol
create function rowtocol(@id int)
returns varchar(1000)
as
begin
declare @str varchar(1000)
set @str = ''
select @str=@str+','+名称 from 表A where 代号 = @id
set @str = stuff(@str,1,1,'')
return @str
end
select 代号,dbo.rowtocol(代号) 名称 from 表A group by 代号
-----------------------------------------------------------------------------
--带符号合并行列转换
--有表t,其数据如下:
a b
1 1
1 2
1 3
2 1
2 2
3 1
--如何转换成如下结果:
a b
1 1,2,3
2 1,2
3 1
create table tb
(
a int,
b int
)
insert into tb(a,b) values(1,1)
insert into tb(a,b) values(1,2)
insert into tb(a,b) values(1,3)
insert into tb(a,b) values(2,1)
insert into tb(a,b) values(2,2)
insert into tb(a,b) values(3,1)
go
if object_id('pubs..f_hb') is not null
drop function f_hb
go
--创建一个合并的函数
create function f_hb(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(b as varchar) from tb where a = @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct a ,dbo.f_hb(a) as b from tb
drop table tb
--结果
a b
----------- ------
1 1,2,3
2 1,2
3 1
(所影响的行数为 3 行)
多个前列的合并
数据的原始状态如下:
ID PR CON OP SC
001 p c 差 6
001 p c 好 2
001 p c 一般 4
002 w e 差 8
002 w e 好 7
002 w e 一般 1
===========================
用SQL语句实现,变成如下的数据
ID PR CON OPS
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)
if object_id('pubs..tb') is not null
drop table tb
go
create table tb
(
id varchar(10),
pr varchar(10),
con varchar(10),
op varchar(10),
sc int
)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '差', 6)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '好', 2)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '一般', 4)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '差', 8)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '好', 7)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '一般', 1)
go
if object_id('pubs..test') is not null
drop table test
go
select ID,PR,CON , OPS = op + '(' + cast(sc as varchar(10)) + ')' into test from tb
--创建一个合并的函数
if object_id('pubs..f_hb') is not null
drop function f_hb
go
create function f_hb(@id varchar(10),@pr varchar(10),@con varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(OPS as varchar) from test where id = @id and @pr = pr and @con = con
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct id ,pr , con , dbo.f_hb(id,pr,con) as OPS from test
drop table tb
drop table test
--结果
id pr con OPS
---------- ---------- ---------- -------------------
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)
(所影响的行数为 2 行)
-----------------------------------------------------
create table ta(id int,username varchar(20))
insert into ta select 1,'aa'
insert into ta select 2,'bb'
insert into ta select 3,'cc'
insert into ta select 4,'dd'
insert into ta select 2,'ee'
insert into ta select 3,'ff'
insert into ta select 2,'gg'
create function test(@id int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
select @s = isnull(@s,'')+','+username from ta where id = @id
return stuff(@s,1,1,'')
end
select distinct id,dbo.test(id)username from ta
id username
----------- -------------------
1 aa
2 bb,ee,gg
3 cc,ff
4 dd
(所影响的行数为 4 行)
--drop table ta
--drop function test
------------------------------------------------------------------
create table ta(代号 int,名称 varchar(20))
insert into ta select 1,'a'
insert into ta select 1,'b'
insert into ta select 1,'c'
insert into ta select 2,'a'
insert into ta select 2,'a'
insert into ta select 2,'d'
insert into ta select 3,'d'
insert into ta select 3,'c'
insert into ta select 3,'e'
create function test(@id int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
select @s = isnull(@s,'')+','+名称 from ta where 代号 = @id group by 代号,名称--加一个组
return stuff(@s,1,1,'')
end
select distinct 代号,dbo.test(代号)名称 from ta
--drop table ta
--drop function test
代号 名称
----------- -----------
1 a,b,c
2 a,d
3 c,d,e
(所影响的行数为 3 行)
--------------------------------------------------------------------
--函数
create table A(代号 int, 名称 varchar(10))
insert A select 1, 'a'
union all select 1, 'b'
union all select 1, 'c'
union all select 2, 'a'
union all select 2, 'a'
union all select 2, 'd'
union all select 3, 'd'
union all select 3, 'c'
union all select 3, 'e'
create function fun(@代号 int)
returns varchar(100)
as
begin
declare @re varchar(100)
set @re=''
select @re=@re+','+名称 from A where 代号=@代号 group by 名称
return(stuff(@re, 1, 1, ''))
end
select 代号, 名称=dbo.fun(代号)
from
A
group by 代号
--result
代号 名称
----------- ----------------------------------------------------------------------------------------------------
1 a,b,c
2 a,d
3 c,d,e
(3 row(s) affected)
---------------------------------------------------
--临时表
create table A(代号 int, 名称 varchar(10))
insert A select 1, 'a'
union all select 1, 'b'
union all select 1, 'c'
union all select 2, 'a'
union all select 2, 'a'
union all select 2, 'd'
union all select 3, 'd'
union all select 3, 'c'
union all select 3, 'e'
select 代号, cast(名称 as varchar(100)) as 名称 into #A
from A
group by 代号, 名称
order by 代号
declare @代号 int, @名称 varchar(100)
update #A set
@名称=case when 代号=@代号 then @名称+','+名称 else 名称 end,
@代号=代号,
名称=@名称
select 代号, 名称=max(名称) from #A
group by 代号
--result
代号 名称
----------- ----------------------------------------------------------------------------------------------------
1 a,b,c
2 a,d
3 c,d,e
(3 row(s) affected)
本文介绍了如何在SQL中合并相同代号或部门的名称,使用用户定义函数和GROUP BY语句来实现。通过示例展示了如何创建和使用函数,以达到将多个名称合并为一个字符串的效果。
1259

被折叠的 条评论
为什么被折叠?



