表结构
A
------------------
apple 5
apple 6
apple 7
pear 1
pear 2
pear 3
--------------------
要得到TABE B
--------------------
apple 5,6,7
pear 1,2,3
--------------------
...全文
7439打赏收藏
求SQL 按字段 求字符串的和 Group by 解决就揭贴在线等谢谢
表结构 A ------------------ apple 5 apple 6 apple 7 pear 1 pear 2 pear 3 -------------------- 要得到TABE B -------------------- apple 5,6,7 pear 1,2,3 --------------------
lengxiaowei(小伟) 结果:
id a b
1 apple ,5,6,7
2 pear 1,2,3,
使用了dbo.jionstr(a),很正确
---------------------------------------
tdtjjiao(给分) 结果:
id a b
1 apple 5,6,7
2 pear 1,2,3
只用SQL 方法最简单最 实用 个人比较喜欢这样的
-----------------------------------------
create table t (id int Identity(1,1), a varchar(10), b int)
insert t (a, b) values ('apple', 5)
insert t (a, b) values ('apple', 6)
insert t (a, b) values ('apple', 7)
insert t (a, b) values ('pear', 1)
insert t (a, b) values ('pear', 2)
insert t (a, b) values ('pear', 3)
alter function jionstr (@str varchar(50))
returns varchar(100)
as
begin
declare @str1 varchar(100)
set @str1=''
select @str1=@str1+cast(b as varchar(10))+',' from t where a=@str
return @str1
end
--加入测试数据
create table #t (id int Identity(1,1), a varchar(10), b int)
insert #t (a, b) values ('apple', 5)
insert #t (a, b) values ('apple', 6)
insert #t (a, b) values ('apple', 7)
insert #t (a, b) values ('pear', 1)
insert #t (a, b) values ('pear', 2)
insert #t (a, b) values ('pear', 3)
create table #t1 (id int Identity(1,1), a varchar(10), b varchar(8000) not null, c int)
Insert #t1 (a, b, c) select a, '', count(*) from #t group by a
declare @count int, @min int, @max int, @a varchar(10), @min1 int, @max1 int
set @min = 1
set @min1 = 1
select @max=Max(id) from #t1
while @min <= @max
begin
Select @a = a from #t1 where id = @min order by id
Select @max1 = max(id) from #t where a = @a
while @min1 <= @max1
begin
if @min1 < @max1
begin
update #t1 set #t1.b = #t1.b + convert(varchar(1), s.b)+',' from #t s where s.id = @min1 and #t1.a = @a
end
if @min1 = @max1
begin
update #t1 set #t1.b = #t1.b + convert(varchar(1), s.b) from #t s where s.id = @min1 and #t1.a = @a
end
set @min1 = @min1 + 1
end
set @min = @min + 1
end
--结果
select id, a, b from #t1
id a b
1 apple 5,6,7
2 pear 1,2,3
----測試環境 --------將同組的字段用逗號連接起來
create table a(id1 int,id2 varchar(50) )
insert into a
select 2, 1 union all
select 2 , 2 union all
select 2 , 3 union all
select 2 ,3 union all
select 2 , 5 union all
select 3 , 2 union all
select 3 , 3 union all
select 3, 5 union all
select 3 , 5
if exists (select 1 from sysobjects where name='combine' and xtype='fn')
drop Function combine
------編寫函數實現整合功能
create FUNCTION combine(@vchA varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+','+id2 FROM a WHERE id1=@vchA
RETURN(substring(@r,2,8000))
END
GO
-------執行語句
select id1,id2=dbo.combine(id1) from a group by id1
----刪除測試環境
---drop function combine
---drop table a
-------create by talantlee---------
--------------逗號問題的還原問題----
----------------------------
---測試環境
--要求從--表 a(id1,id2)
drop table a,b,#a
create table a (id1 int,id2 varchar(100))
insert into a
select 2, ' 1,2,3' union all
select 2, '3,5' union all
select 3, '2,3,5' union all
select 3, '5'