一个查询问题

jackystar 2004-04-09 09:43:27
数据

A B

1 a
1 b
1 c
2 d
2 e
3 f

我要查询结果为
1 a,b,c
2 d,e
3 f
这个查询语句该如何写?

...全文
36 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangsu 2004-04-09
  • 打赏
  • 举报
回复
這麼快 !!!
遲發了一步!
xiangsu 2004-04-09
  • 打赏
  • 举报
回复

declare @temp table (A varchar(4),B varchar(4))
declare @temp2 table (A varchar(4),B varchar(8000))
declare @A varchar(4)
declare @s varchar(8000)
insert @temp
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'

declare cur_1 cursor for select distinct A from @temp
open cur_1
fetch cur_1 into @A
while @@fetch_status=0
begin
set @s = ''
select @s = @s + ',' + B
from @temp where A=@A
set @s=substring(@s,2,8000)
insert @temp2
select @A,@s
fetch cur_1 into @A
end
deallocate cur_1
select * from @temp2

a b
---- -----------
1 a,b,c
2 d,e
3 f

(影響 3 個資料列)
jackystar 2004-04-09
  • 打赏
  • 举报
回复
谢谢 progress99(如履薄冰)
搞定
zjcxc 元老 2004-04-09
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(a int,b varchar(10))
insert 表 select 1,'a'
union all 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'
go

--创建处理函数
create function f_merg(@id int)
returns varchar(8000)
as
begin
declare @re varchar(8000)
set @re=''
select @re=@re+','+b from 表 where a=@id group by b
return(substring(@re,2,8000))
end
go

--调用实现查询
select a,b=dbo.f_merg(a) from 表 group by a
go

--删除测试
drop table 表
drop function f_merg

/*--测试结果
a b
----------- -----------
1 a,b,c
2 d,e
3 f

(所影响的行数为 3 行)
--*/
zjcxc 元老 2004-04-09
  • 打赏
  • 举报
回复
--上面的函数没有去重复值,改一下:

--创建处理函数
create function f_merg(@id int)
returns varchar(8000)
as
begin
declare @re varchar(8000)
set @re=''
select @re=@re+','+b from 表 where a=@id group by b
return(substring(@re,2,8000))
end
go

--调用实现查询
select a,b=dbo.f_merg(a) from 表 group by a
zjcxc 元老 2004-04-09
  • 打赏
  • 举报
回复
--创建处理函数
create function f_merg(@id int)
returns varchar(8000)
as
begin
declare @re varchar(8000)
set @re=''
select @re=@re+','+b from 表 where a=@id
return(substring(@re,2,8000))
end
go

--调用实现查询
select a,b=dbo.f_merg(a) from 表 group by a
rouqu 2004-04-09
  • 打赏
  • 举报
回复
顶楼上
progress99 2004-04-09
  • 打赏
  • 举报
回复
樓主自己修改一下就OK了。
progress99 2004-04-09
  • 打赏
  • 举报
回复
參考:

--函數,合並字符串欄位
--測試數據
/*
CREATE table person_info(dept_name varchar(10),position_name varchar(10),person_name varchar(10))
INSERT INTO person_info
SELECT '資訊部','軟件編碼員','小李'
UNION ALL SELECT '資訊部','軟件編碼員','小王'
UNION ALL SELECT '行政部','秘書','小芳'
*/


--合並函數
CREATE FUNCTION FunMergeCharField(@vchA varchar(10),@vchB varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+','+person_name FROM person_info WHERE dept_name=@vchA and position_name=@vchB
RETURN(substring(@r,2,8000))
END
GO


--删除测试
DROP TABLE person_info
DROP FUNCTION FunMergeCharField



--調用
SELECT dept_name,position_name,在職人員=dbo.FunMergeCharField(dept_name,position_name)
FROM person_info
GROUP BY dept_name,position_name
go



xiangsu 2004-04-09
  • 打赏
  • 举报
回复
我來看看....

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧