这样显示,SQL 怎么写呢?

sunnypjd 2007-12-20 04:12:32
id name
1 a
1 b
1 c
2 a
2 c

显示为
id name
1 a,b,c
2 a,c
...全文
56 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
windlysnowly 2007-12-20
  • 打赏
  • 举报
回复
写个最简单的
create table tabletest(id int,name char(20))

insert into tabletest values
(1,'a'),
(1,'b'),
(1,'c'),
(2,'a'),
(2,'c')

with m (id,b1,b2,b3) as (
select id,min(case name when 'a' then name else null end) as b1,
min(case name when 'b' then name else null end) as b2,
min(case name when 'c' then name else null end) as b3
from tabletest
group by id)
select id,value(B1,'') ¦ ¦value(B2,'') ¦ ¦value(B3,'') from M
---------------------------------------------------------------------------------
------------------------------------------------------------------------------

Results for a single query are displayed on the Query Results tab.
2 row(s) returned successfully.
windlysnowly 2007-12-20
  • 打赏
  • 举报
回复
写个最简单的
create table tabletest(id int,name char(20))

insert into tabletest values
(1,'a'),
(1,'b'),
(1,'c'),
(2,'a'),
(3,'c')

with m (id,b1,b2,b3) as (
select id,min(case name when 'a' then name else null end) as b1,
min(case name when 'b' then name else null end) as b2,
min(case name when 'c' then name else null end) as b3
from tabletest
group by id)
select id,value(B1,'')||value(B2,'')||value(B3,'') from M
---------------------------------------------------------------------------------
------------------------------------------------------------------------------

Results for a single query are displayed on the Query Results tab.
2 row(s) returned successfully.
JL99000 2007-12-20
  • 打赏
  • 举报
回复
if object_id('t_merge') is not null
drop table t_merge
if object_id('dbo.f_merge') is not null
drop function dbo.f_merge
go
create table t_merge(id int,name_ch varchar(100))
insert t_merge
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'a' union all
select 2,'c'
select * from t_merge

go
create function f_merge(@id int)
returns varchar(100)
as
begin
declare @s varchar(100)
set @s=''
select @s =isnull(@s,'')+name_ch+',' from t_merge where id=@id
set @s=substring(@s,1,len(@s)-1)
return @s
end

go

select id ,dbo.f_merge(id) from t_merge group by id
leo_lesley 2007-12-20
  • 打赏
  • 举报
回复
--例子
create table tt(id int,name varchar(1))
insert tt
select 1 ,'a'
union select 1 ,'b'
union select 1 ,'c'
union select 2 ,'a'
union select 2 ,'c'
go
create function f1(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)

set @str=''
select @str=@str+','+[name]
from tt
where id=@id
set @str=stuff(@str,1,1,'')
return @str
end
go

select id,dbo.f1(id)
from tt
group by id

go
drop table tt
drop function f1
leo_lesley 2007-12-20
  • 打赏
  • 举报
回复
create function f1(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)

set @str=''
select @str=@str+','+[name]
from 表名
where id=@id
set @str=stuff(@str,1,1,'')
return @str
end
go

select id,dbo.f1(id)
from 表名
group by id
dawugui 2007-12-20
  • 打赏
  • 举报
回复
合并列值
原著:邹建
改编:爱新觉罗.毓华 2007-12-16 广东深圳

表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc

需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)

1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
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 int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value FROM tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO

-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id

drop table tb
drop function dbo.f_str

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/

--2、另外一种函数.
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 f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb

drop table tb
drop function dbo.f_hb

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/

2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
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 * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb

/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc

(2 行受影响)
*/
dawugui 2007-12-20
  • 打赏
  • 举报
回复
/*
带符号合并行列转换(2007-11-19于海南三亚)

有表tb,其数据如下:
a b
1 1
1 2
1 3
2 1
2 2
3 1
如何转换成如下结果:
a b
1 1,2,3
2 1,2
3 1
*/

create table tb
(
a int,
b int
)
insert into tb(a,b) values(1,1)
insert into tb(a,b) values(1,2)
insert into tb(a,b) values(1,3)
insert into tb(a,b) values(2,1)
insert into tb(a,b) values(2,2)
insert into tb(a,b) values(3,1)
go

--创建一个合并的函数
create function f_hb(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(b as varchar) from tb where a = @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct a ,dbo.f_hb(a) as b from tb

drop table tb
drop function f_hb

/*
结果
a b
----------- ------
1 1,2,3
2 1,2
3 1

(所影响的行数为 3 行)
*/

----------------------------------------------------
/*
多个前列的合并
数据的原始状态如下:
ID PR CON OP SC
001 p c 差 6
001 p c 好 2
001 p c 一般 4
002 w e 差 8
002 w e 好 7
002 w e 一般 1
用SQL语句实现,变成如下的数据
ID PR CON OPS
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)
*/

create table tb
(
id varchar(10),
pr varchar(10),
con varchar(10),
op varchar(10),
sc int
)

insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '差', 6)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '好', 2)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '一般', 4)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '差', 8)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '好', 7)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '一般', 1)
go

--创建一个合并的函数
create function f_hb(@id varchar(10) , @pr varchar(10) , @con varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(OP as varchar) + '('
+ cast(sc as varchar) + ')'
from tb where id = @id and @pr = pr and @con = con
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct id , pr , con , dbo.f_hb(id,pr,con) as ops from tb

drop table tb
drop function f_hb

/*
结果
id pr con ops
---------- ---------- ---------- ------------------
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)

(所影响的行数为 2 行)
*/

----------------------------------------------------
/*如何将一列中所有的值一行显示
数据源
a
b
c
d
e
结果
a,b,c,d,e
*/

create table tb(col varchar(20))
insert tb values ('a')
insert tb values ('b')
insert tb values ('c')
insert tb values ('d')
insert tb values ('e')
go

--方法一
declare @sql varchar(1000)
set @sql = ''
select @sql = @sql + t.col + ',' from (select col from tb) as t
set @sql='select result = ''' + @sql + ''''
exec(@sql)
/*
result
----------
a,b,c,d,e,
*/

--方法二
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + col from tb
print @output
/*
a,b,c,d,e
*/

drop table tb

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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