求多行转一行SQL语句

时间:2021-10-30 18:10:08
0003050000BGZ0 主承销商 华宝信托投资有限责任公司
0003050000BGZ0 副主承销商 联合证券有限责任公司
0003050000BGZ0 副主承销商 宝钢集团财务有限责任公司
0003050000BGZ0 副主承销商 华夏证券有限责任公司
0003050000BGZ0 副主承销商 长江证券有限责任公司

这样的表如果变成下表
0003050000BGZ0 主承销商   华宝信托投资有限责任公司
0003050000BGZ0 副主承销商 联合证券有限责任公司、宝钢集团财务有限责任公司、华夏证券有限责任公司、长江证券有限责任公司

由五条数据变成二条数据

7 个解决方案

#1


用函数,假设3列的列名分别是a,b,c

create function f_addstr(@a char(12),@b varchar(10))
returns varchar(8000)
as
begin
  declare @str varchar(8000)
  set @str = ''
  select @str = @str + '、' + [c] from tablename where a = @a and b = @b
  set @str = right(@str , len(@str) - 1)
  return(@str)
End
go

--调用语句:
select a,b,dbo.f_addstr(a,b)
from tablename
group by a,b

#2


--带符号合并行列转换

--有表t,其数据如下:
  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

--结果
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)

if object_id('pubs..tb') is not null
   drop table tb
go

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

if object_id('pubs..test') is not null
   drop table test
go
select ID,PR,CON , OPS = op + '(' + cast(sc as varchar(10)) + ')' into test from tb

--创建一个合并的函数
if object_id('pubs..f_hb') is not null
   drop function f_hb
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(OPS as varchar) from test 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 test

drop table tb
drop table test

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

(所影响的行数为 2 行)



#3


if object_id('pubs..tb') is not null
   drop table tb
go

create table tb
(
code  varchar(20),
name  varchar(20),
demo  varchar(100)
)

insert into tb(code,name,demo) values('0003050000BGZ0','主承销商','华宝信托投资有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','联合证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','宝钢集团财务有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','华夏证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','长江证券有限责任公司')

--创建一个合并的函数
if object_id('pubs..f_hb') is not null
   drop function f_hb
go

create function f_hb(@code varchar(20))
returns varchar(8000)
as
begin
  declare @str varchar(8000)
  set @str = ''
  select @str = @str + '、' + cast(demo as varchar) from tb where code = @code and name = '副主承销商'
  set @str = right(@str , len(@str) - 1)
  return(@str)
End
go

--调用自定义函数得到结果:
select code , demo from tb where name = '主承销商'
union all 
select distinct code ,dbo.f_hb(code) as demo from tb 
drop table tb

code           demo                    
-------------- ------------------------
0003050000BGZ0 华宝信托投资有限责任公司
0003050000BGZ0 联合证券有限责任公司、宝钢集团财务有限责任公司、华夏证券有限责任公司、长江证券有限责任公司

(所影响的行数为 2 行)

#4


if object_id('pubs..tb') is not null
   drop table tb
go

create table tb
(
code  varchar(20),
name  varchar(20),
demo  varchar(100)
)

insert into tb(code,name,demo) values('0003050000BGZ0','主承销商','华宝信托投资有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','联合证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','宝钢集团财务有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','华夏证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','长江证券有限责任公司')

--创建一个合并的函数
if object_id('pubs..f_hb') is not null
   drop function f_hb
go

create function f_hb(@code varchar(20),@name varchar(20))
returns varchar(8000)
as
begin
  declare @str varchar(8000)
  set @str = ''
  select @str = @str + '、' + cast(demo as varchar) from tb where code = @code and name = @name
  set @str = right(@str , len(@str) - 1)
  return(@str)
End
go

--调用自定义函数得到结果:
select distinct code ,dbo.f_hb(code,name) as demo from tb 
drop table tb

code           demo                    
-------------- ------------------------
0003050000BGZ0 华宝信托投资有限责任公司
0003050000BGZ0 联合证券有限责任公司、宝钢集团财务有限责任公司、华夏证券有限责任公司、长江证券有限责任公司

(所影响的行数为 2 行)

#5


呵呵,都很快嘛
只能顶一下了

#6


create table T(col1 varchar(20), col2 nvarchar(20), col3 nvarchar(50))
insert T select '0003050000BGZ0', '主承销商', '华宝信托投资有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '联合证券有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '宝钢集团财务有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '华夏证券有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '长江证券有限责任公司'

create function fun(@col1 varchar(20), @col2 nvarchar(20))
returns nvarchar(1000)
as 
begin
declare @re nvarchar(1000)
set @re=''
select @re=@re+col3+'、' from T where col1=@col1 and col2=@col2

select @re=left(@re, len(@re)-1)
return @re
end

select col1, col2, dbo.fun(col1, col2) as col3 from T group by col1, col2

--result
col1                 col2                 col3                                                                                                                                                                                                                                                             
-------------------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
0003050000BGZ0       副主承销商                联合证券有限责任公司、宝钢集团财务有限责任公司、华夏证券有限责任公司、长江证券有限责任公司
0003050000BGZ0       主承销商                 华宝信托投资有限责任公司

(2 row(s) affected)

#7


用动态查询可以解决你的问题@@

#1


用函数,假设3列的列名分别是a,b,c

create function f_addstr(@a char(12),@b varchar(10))
returns varchar(8000)
as
begin
  declare @str varchar(8000)
  set @str = ''
  select @str = @str + '、' + [c] from tablename where a = @a and b = @b
  set @str = right(@str , len(@str) - 1)
  return(@str)
End
go

--调用语句:
select a,b,dbo.f_addstr(a,b)
from tablename
group by a,b

#2


--带符号合并行列转换

--有表t,其数据如下:
  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

--结果
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)

if object_id('pubs..tb') is not null
   drop table tb
go

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

if object_id('pubs..test') is not null
   drop table test
go
select ID,PR,CON , OPS = op + '(' + cast(sc as varchar(10)) + ')' into test from tb

--创建一个合并的函数
if object_id('pubs..f_hb') is not null
   drop function f_hb
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(OPS as varchar) from test 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 test

drop table tb
drop table test

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

(所影响的行数为 2 行)



#3


if object_id('pubs..tb') is not null
   drop table tb
go

create table tb
(
code  varchar(20),
name  varchar(20),
demo  varchar(100)
)

insert into tb(code,name,demo) values('0003050000BGZ0','主承销商','华宝信托投资有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','联合证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','宝钢集团财务有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','华夏证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','长江证券有限责任公司')

--创建一个合并的函数
if object_id('pubs..f_hb') is not null
   drop function f_hb
go

create function f_hb(@code varchar(20))
returns varchar(8000)
as
begin
  declare @str varchar(8000)
  set @str = ''
  select @str = @str + '、' + cast(demo as varchar) from tb where code = @code and name = '副主承销商'
  set @str = right(@str , len(@str) - 1)
  return(@str)
End
go

--调用自定义函数得到结果:
select code , demo from tb where name = '主承销商'
union all 
select distinct code ,dbo.f_hb(code) as demo from tb 
drop table tb

code           demo                    
-------------- ------------------------
0003050000BGZ0 华宝信托投资有限责任公司
0003050000BGZ0 联合证券有限责任公司、宝钢集团财务有限责任公司、华夏证券有限责任公司、长江证券有限责任公司

(所影响的行数为 2 行)

#4


if object_id('pubs..tb') is not null
   drop table tb
go

create table tb
(
code  varchar(20),
name  varchar(20),
demo  varchar(100)
)

insert into tb(code,name,demo) values('0003050000BGZ0','主承销商','华宝信托投资有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','联合证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','宝钢集团财务有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','华夏证券有限责任公司')
insert into tb(code,name,demo) values('0003050000BGZ0','副主承销商','长江证券有限责任公司')

--创建一个合并的函数
if object_id('pubs..f_hb') is not null
   drop function f_hb
go

create function f_hb(@code varchar(20),@name varchar(20))
returns varchar(8000)
as
begin
  declare @str varchar(8000)
  set @str = ''
  select @str = @str + '、' + cast(demo as varchar) from tb where code = @code and name = @name
  set @str = right(@str , len(@str) - 1)
  return(@str)
End
go

--调用自定义函数得到结果:
select distinct code ,dbo.f_hb(code,name) as demo from tb 
drop table tb

code           demo                    
-------------- ------------------------
0003050000BGZ0 华宝信托投资有限责任公司
0003050000BGZ0 联合证券有限责任公司、宝钢集团财务有限责任公司、华夏证券有限责任公司、长江证券有限责任公司

(所影响的行数为 2 行)

#5


呵呵,都很快嘛
只能顶一下了

#6


create table T(col1 varchar(20), col2 nvarchar(20), col3 nvarchar(50))
insert T select '0003050000BGZ0', '主承销商', '华宝信托投资有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '联合证券有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '宝钢集团财务有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '华夏证券有限责任公司'
union all select '0003050000BGZ0', '副主承销商', '长江证券有限责任公司'

create function fun(@col1 varchar(20), @col2 nvarchar(20))
returns nvarchar(1000)
as 
begin
declare @re nvarchar(1000)
set @re=''
select @re=@re+col3+'、' from T where col1=@col1 and col2=@col2

select @re=left(@re, len(@re)-1)
return @re
end

select col1, col2, dbo.fun(col1, col2) as col3 from T group by col1, col2

--result
col1                 col2                 col3                                                                                                                                                                                                                                                             
-------------------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
0003050000BGZ0       副主承销商                联合证券有限责任公司、宝钢集团财务有限责任公司、华夏证券有限责任公司、长江证券有限责任公司
0003050000BGZ0       主承销商                 华宝信托投资有限责任公司

(2 row(s) affected)

#7


用动态查询可以解决你的问题@@