求一个多行转为一行显示的sql--在线等!!!

lmbbs_cd 2014-01-21 04:56:36
表t1
字段 id,desc

数据:

id desc
1 aaa
1 bbb
1 ccc
2 xxx
2 yyy

想查出数据的形式:

id desc
1 aaa,bbb,ccc
2 xxx,yyy

多谢!!
...全文
109 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LongRui888 2014-01-21
  • 打赏
  • 举报
回复
上面是适合2005及以上的版本。 下面的适合2000的:

--drop table t1
--go

create table t1(id int,[desc] varchar(100))


insert into t1
select 1    ,'aaa' union all
select 1    ,'bbb' union all
select 1    ,'ccc' union all
select 2    ,'xxx' union all
select 2    ,'yyy'
go

--建立函数
if OBJECT_ID('fn_mergeStr') is not null
   drop function fn_mergeStr
go

create function dbo.fn_mergeStr (@id int)
returns varchar(1000)
as
begin

declare @str varchar(1000)

set @str = ''

select @str = @str + ','+[desc]
from t1
where id = @id

return stuff(@str,1,1,'')
end
go


--查询
select distinct 
       id,
       dbo.fn_mergeStr(id) [desc]
from t1
/*
id	desc
1	aaa,bbb,ccc
2	xxx,yyy
*/
LongRui888 2014-01-21
  • 打赏
  • 举报
回复
试试这个:

--drop table t1
--go

create table t1(id int,[desc] varchar(100))


insert into t1
select 1    ,'aaa' union all
select 1    ,'bbb' union all
select 1    ,'ccc' union all
select 2    ,'xxx' union all
select 2    ,'yyy'
go

select distinct 
       id,
       STUFF((select ','+[desc] from t1 t where t1.id = t.id for xml path('')),
             1,1,'') as [desc]
from t1
/*
id	desc
1	aaa,bbb,ccc
2	xxx,yyy
*/
發糞塗牆 2014-01-21
  • 打赏
  • 举报
回复
2000、非2000都有
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-21 16:57:12
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[t1]
if object_id('[t1]') is not null drop table [t1]
go 
create table [t1]([id] int,[desc] varchar(3))
insert [t1]
select 1,'aaa' union all
select 1,'bbb' union all
select 1,'ccc' union all
select 2,'xxx' union all
select 2,'yyy'
--------------开始查询--------------------------
--2005
select a.id,
stuff((select ','+[desc] from [t1] b 
       where b.id=a.id 
       for xml path('')),1,1,'') 'desc'
from [t1] a
group by  a.id

--2000
--创建函数
if object_id('F_Str') is not null
    drop function F_Str
go
create function F_Str(@id int)
returns nvarchar(100)
as
begin
    declare @S nvarchar(100)
    select @S=isnull(@S+',','')+[desc] from [t1] where id=@id
    return @S
end
go
Select distinct id,[desc]=dbo.F_Str(id) FROM [t1]

----------------结果----------------------------
/* 
id          desc
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1           aaa,bbb,ccc
2           xxx,yyy

(2 row(s) affected)

id          desc
----------- ----------------------------------------------------------------------------------------------------
1           aaa,bbb,ccc
2           xxx,yyy

*/
發糞塗牆 2014-01-21
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-21 16:57:12
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[t1]
if object_id('[t1]') is not null drop table [t1]
go 
create table [t1]([id] int,[desc] varchar(3))
insert [t1]
select 1,'aaa' union all
select 1,'bbb' union all
select 1,'ccc' union all
select 2,'xxx' union all
select 2,'yyy'
--------------开始查询--------------------------

select a.id,
stuff((select ','+[desc] from [t1] b 
       where b.id=a.id 
       for xml path('')),1,1,'') 'desc'
from [t1] a
group by  a.id
----------------结果----------------------------
/* 
id          desc
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1           aaa,bbb,ccc
2           xxx,yyy
*/

34,837

社区成员

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

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