sql 合计列当中的内容
有如下表结构idname1aa2bb1cc1dd2ee请问怎么用sql语句把1和2的内容合并,即如下:idname1aaccdd2bbee请高手解决一下,解决了有分...
有如下表结构
id name
1 aa
2 bb
1 cc
1 dd
2 ee
请问怎么用sql语句把1和2的内容合并,即如下:
id name
1 aaccdd
2 bbee
请高手解决一下,解决了有分 展开
id name
1 aa
2 bb
1 cc
1 dd
2 ee
请问怎么用sql语句把1和2的内容合并,即如下:
id name
1 aaccdd
2 bbee
请高手解决一下,解决了有分 展开
4个回答
展开全部
--写一个聚合函数:
create function dbo.fn_Merge(@id int)
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+';'+txt from tb where id=@id
return stuff(@r,1,1,'')
end
go
-- 调用函数
select id, dbo.fn_Merge(id) as txt from tb group by id
go
drop table tb
drop function fn_Merge
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/linjcong/archive/2007/08/28/1761502.aspx
create function dbo.fn_Merge(@id int)
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+';'+txt from tb where id=@id
return stuff(@r,1,1,'')
end
go
-- 调用函数
select id, dbo.fn_Merge(id) as txt from tb group by id
go
drop table tb
drop function fn_Merge
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/linjcong/archive/2007/08/28/1761502.aspx
展开全部
好像没有这样的函数吧,可能需要你自己来写代码实现了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
create Table t(id int,name varchar(20))
insert t select '1','aa'
union all select '2','bb'
union all select '1','cc'
union all select '1','dd'
union all select '2','ee'
go
--处理分组合并函数学
Create Function JoinStr(@SNO as varchar(20))
returns varchar(200)
begin
declare @s as varchar(8000)
set @s=''
select @s=@s+','+ltrim(rtrim(name)) from
(
select name from t where id=@SNO
)
A
set @s=stuff(@s,1,1,'')
return @s
end
go
--查询语句
select id, replace(dbo.JoinStr(id),',','') as name from t group by id
drop table t
drop function JoinStr
-----------------------------------------
(5 row(s) affected)
id name
----------- ------------------
1 aaccdd
2 bbee
(2 row(s) affected)
insert t select '1','aa'
union all select '2','bb'
union all select '1','cc'
union all select '1','dd'
union all select '2','ee'
go
--处理分组合并函数学
Create Function JoinStr(@SNO as varchar(20))
returns varchar(200)
begin
declare @s as varchar(8000)
set @s=''
select @s=@s+','+ltrim(rtrim(name)) from
(
select name from t where id=@SNO
)
A
set @s=stuff(@s,1,1,'')
return @s
end
go
--查询语句
select id, replace(dbo.JoinStr(id),',','') as name from t group by id
drop table t
drop function JoinStr
-----------------------------------------
(5 row(s) affected)
id name
----------- ------------------
1 aaccdd
2 bbee
(2 row(s) affected)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
喂 楼上的 select @s=@s+','+ltrim(rtrim(name)) from
(
select name from t where id=@SNO
)
A
改成
select @s=@s+ltrim(rtrim(name)) from
(
select name from t where id=1
)
A
后面就不用去逗号多此一举了
仅仅建议
(
select name from t where id=@SNO
)
A
改成
select @s=@s+ltrim(rtrim(name)) from
(
select name from t where id=1
)
A
后面就不用去逗号多此一举了
仅仅建议
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询