SQL语句实现横排?
有一个表:
BH GZLB JE
----------------------
001 A 100
001 B 150
001 C 110
002 A 99
002 B 180
002 C 150
003 A 160
003 B 170
003 C 130
用SQL语句如何将上面的表变成下面横向的排列方式呢。
BH A B C
-------------------
001 100 150 110
002 99 180 150
003 160 170 130
(GZLB 里面的字段是动态的,不是只有A,B,C 可能还有A,B,C,D,E...)
---------------------------------------------------------------
if exists(select name from sysobjects where name='tblA' and xtype='U')
drop table tblA
create table tblA(
BH char(3) not null,
GZLB varchar(3) not null,
JE int not null
)
go
insert tblA values('001', 'A', 100)
insert tblA values('001', 'B', 150)
insert tblA values('001', 'C', 110)
insert tblA values('002', 'A', 99)
insert tblA values('002', 'B', 180)
insert tblA values('002', 'C', 150)
insert tblA values('003', 'A', 160)
insert tblA values('003', 'B', 170)
insert tblA values('003', 'C', 130)
declare @sql varchar(600)
set @sql = 'select'
select distinct GZLB as 'GZLB' into tmp from tblA
select @sql = @sql +' max(case GZLB when '''+ GZLB +''' then JE else null end) as '''+ GZLB +''',' from tmp
set @sql = substring(@sql, 1, len(@sql)-1) +' from tblA group by BH'
exec(@sql)
---------------------------------------------------------------
用游标
declare @sqlstr varchar(2000)
declare @gzlb
select sqlstr='select bh'
declare cgzlb cursor for select gzlb from table
create table #a
(bh char(3))
open cgzlb
fetch next from cgzlb into @gzlb
while (fetch_status<>-1)
begin
exec('alter table #a add '+@gzlb +' char(1)')
exec('insert into #a (bh,'+@gzlb+' select bh,je from table where gzlb='''+@gzlb+'''')
select @sqlstr=@sqlstr+',sum(isnull('+@gzlb+'))'
fetch next from cgzlb into @gzlb
end
close cgzlb
deallocate cgzlb
select @sqlstr=@sqlstr+' from #a group by bh order by bh
execute(@sqlstr)
---------------------------------------------------------------
declare @sql varchar(6000)
set @sql = 'select '
select @sql = @sql +' sum(case GZLB when '''+ GZLB +''' then JE else null end) ['+ GZLB +'],' from (select distinct GZLB as GZLB from tblA) tmp
set @sql = substring(@sql, 1, len(@sql)-1) +' from tblA group by BH'
exec(@sql)
有一个表:
BH GZLB JE
----------------------
001 A 100
001 B 150
001 C 110
002 A 99
002 B 180
002 C 150
003 A 160
003 B 170
003 C 130
用SQL语句如何将上面的表变成下面横向的排列方式呢。
BH A B C
-------------------
001 100 150 110
002 99 180 150
003 160 170 130
(GZLB 里面的字段是动态的,不是只有A,B,C 可能还有A,B,C,D,E...)
---------------------------------------------------------------
if exists(select name from sysobjects where name='tblA' and xtype='U')
drop table tblA
create table tblA(
BH char(3) not null,
GZLB varchar(3) not null,
JE int not null
)
go
insert tblA values('001', 'A', 100)
insert tblA values('001', 'B', 150)
insert tblA values('001', 'C', 110)
insert tblA values('002', 'A', 99)
insert tblA values('002', 'B', 180)
insert tblA values('002', 'C', 150)
insert tblA values('003', 'A', 160)
insert tblA values('003', 'B', 170)
insert tblA values('003', 'C', 130)
declare @sql varchar(600)
set @sql = 'select'
select distinct GZLB as 'GZLB' into tmp from tblA
select @sql = @sql +' max(case GZLB when '''+ GZLB +''' then JE else null end) as '''+ GZLB +''',' from tmp
set @sql = substring(@sql, 1, len(@sql)-1) +' from tblA group by BH'
exec(@sql)
---------------------------------------------------------------
用游标
declare @sqlstr varchar(2000)
declare @gzlb
select sqlstr='select bh'
declare cgzlb cursor for select gzlb from table
create table #a
(bh char(3))
open cgzlb
fetch next from cgzlb into @gzlb
while (fetch_status<>-1)
begin
exec('alter table #a add '+@gzlb +' char(1)')
exec('insert into #a (bh,'+@gzlb+' select bh,je from table where gzlb='''+@gzlb+'''')
select @sqlstr=@sqlstr+',sum(isnull('+@gzlb+'))'
fetch next from cgzlb into @gzlb
end
close cgzlb
deallocate cgzlb
select @sqlstr=@sqlstr+' from #a group by bh order by bh
execute(@sqlstr)
---------------------------------------------------------------
declare @sql varchar(6000)
set @sql = 'select '
select @sql = @sql +' sum(case GZLB when '''+ GZLB +''' then JE else null end) ['+ GZLB +'],' from (select distinct GZLB as GZLB from tblA) tmp
set @sql = substring(@sql, 1, len(@sql)-1) +' from tblA group by BH'
exec(@sql)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix