• 全部
  • 基础类
  • 应用实例
  • 新技术前沿

高分请教1个 SQL语句的写法 。急!

edgethinking 2010-09-19 12:21:33
有一个表中的数据为:
declare @T table(afield varchar(50), bfield varchar(100))
Insert @T
Select 'a', 'asd' union
Select 'a', 'rdf' union
Select 'a', 'dmg' union
Select 'b', 'kk' union
Select 'b', 'useh'

现需要用1条不带函数的SQL语句得到如下结果:
a asd,rdf,dmg
b kk,useh

注意:由于开发实际情况,需要直接用SQL语句查询出结果,不能使用函数来得到bfield的字符级联。
也就是说不能用如下的语句:
Select afield, dbo.getstr(afield) from @T
Group by afield
...全文
112 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zsh0809 2010-09-19
DROP TABLE t

CREATE TABLE t(afield varchar(50), bfield varchar(100))
Insert into T
Select 'a', 'asd' union
Select 'a', 'rdf' union
Select 'a', 'dmg' union
Select 'b', 'kk' union
Select 'b', 'useh'

select a.afield,stuff((select ','+b.bfield from t as b where a.afield = b.afield
for xml path('')),1,1,'') as bfield
from t a
group by a.afield

afield bfield
---------------------------------
a asd,dmg,rdf
b kk,useh

(2 row(s) affected)
回复
hao1hao2hao3 2010-09-19

declare @T table(afield varchar(50), bfield varchar(100))
Insert @T
Select 'a', 'asd' union
Select 'a', 'rdf' union
Select 'a', 'dmg' union
Select 'b', 'kk' union
Select 'b', 'useh'

select a.afield,stuff((select ','+b.bfield from @t as b where a.afield = b.afield for xml path('')),1,1,'') as res
from @t a
group by a.afield


结果
a asd,dmg,rdf
b kk,useh

回复
王向飞 2010-09-19
SQL 2005版本以上支持
declare @T table(afield varchar(50), bfield varchar(100))
Insert @T
Select 'a', 'asd' union
Select 'a', 'rdf' union
Select 'a', 'dmg' union
Select 'b', 'kk' union
Select 'b', 'useh'

Select afield from @T
SELECT a.afield,
status =stuff((
select ','+convert(varchar(20),bfield) FROM @T WHERE afield = a.afield for xml path(''))
,1,1,'')
FROM @T a
group by a.afield
回复
喜-喜 2010-09-19
declare @T table(afield varchar(50), bfield varchar(100))
Insert @T
Select 'a', 'asd' union
Select 'a', 'rdf' union
Select 'a', 'dmg' union
Select 'b', 'kk' union
Select 'b', 'useh'


select distinct afield,bfield=stuff((select ','+rtrim(bfield)
from @T where afield=t.afield for xml path('')),1,1,'') from @T t
/*
afield bfield
---------------------
a asd,dmg,rdf
b kk,useh
*/
回复
cxmcxm 2010-09-19
不用函数,只能用循环或游标
--有一个表中的数据为:
declare @T table(afield varchar(50), bfield varchar(100))
Insert @T
Select 'a', 'asd' union
Select 'a', 'rdf' union
Select 'a', 'dmg' union
Select 'b', 'kk' union
Select 'b', 'useh'



declare @T2 table(afield varchar(50), bfield varchar(100),cfield varchar(1000))
insert into @t2 (afield,bfield)
select distinct afield,'' from @t
update a
set bfield=(select top 1 bfield from @t where afield=a.afield order by bfield),
cfield=(select top 1 bfield from @t where afield=a.afield order by bfield)
from @t2 a
while exists(select * from @t a,@t2 b where a.afield=b.afield and a.bfield>b.bfield)
begin
update a
set bfield=(select top 1 bfield from @t where afield=a.afield and bfield>a.bfield order by bfield),
cfield=a.cfield+','+(select top 1 bfield from @t where afield=a.afield and bfield>a.bfield order by bfield)
from @t2 a where exists(select * from @t where afield=a.afield and bfield>a.bfield)

end

select afield,cfield from @t2
回复
edgethinking 2010-09-19
很遗憾,没有SQL2000的答案,看了回复,估计是没有答案了.
不能用函数,是因为效率太低的问题. 本来想用临时表,再用1条SQL来提高速度, 看来得另想办法了.:)
回复
gll062 2010-09-19
学习 !!!!!!!!!!!!!!!!!!
回复
Rotel-刘志东 2010-09-19
declare @T table(afield varchar(50), bfield varchar(100))
Insert @T
Select 'a', 'asd' union
Select 'a', 'rdf' union
Select 'a', 'dmg' union
Select 'b', 'kk' union
Select 'b', 'useh'


select distinct afield,bfield=stuff((select ','+rtrim(bfield)
from @T where afield=t.afield for xml path('')),1,1,'') from @T t
/*
afield bfield
---------------------
a asd,dmg,rdf
b kk,useh
*/
回复
wuguoxian001 2010-09-19
[Quote=引用 6 楼 dawugui 的回复:]
--sql 2000得用函数,2005则参考第二个内容。


SQL code
/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳

描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2……
[/Quote]
OK
回复
dawugui 2010-09-19
--sql 2000得用函数,2005则参考第二个内容。

/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳

描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id value
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

create function dbo.f_str(@id varchar(10)) returns varchar(1000)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + ',' , '') + cast(value as varchar) from tb where id = @id
return @str
end
go

--调用函数
select id , value = dbo.f_str(id) from tb group by id

drop function dbo.f_str
drop table tb


--2、sql2005中的方法
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id

drop table tb


--3、使用游标合并数据
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursor

select * from @t
drop table tb
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2010-09-19 12:21
社区公告
暂无公告