怎样将行转成列

yangyangbeibei 2003-10-20 05:01:58
表a的结构如下
ID year score
001 1999 1
001 2000 2
001 2001 3
002 1999 1
怎样做才能将其转成如下格式
ID 1999 2000 2001
001 1 2 3
002 1
...全文
63 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
请问楼主,如下方式不行么?
测试:
create table a(id char(3),year int,score int)
insert a select '001', 1999, 1
union all select '001', 2000, 2
union all select '001', 2001, 3
union all select '002', 1999, 1


declare @sql varchar(8000)
set @sql = 'select distinct ID'
select @sql = @sql + ',IsNull(cast(sum(case [year] when '''+cast([year] as char(4))+''' then score end) as varchar(10)),'''') ['+cast([year] as char(4))+']'
from (select distinct [year] from a) aa

select @sql =@sql+' from a group by ID '
--print @sql
exec(@sql)


ID 1999 2000 2001
---- ---------- ---------- ----------
001 1 2 3
002 1
yangyangbeibei 2003-10-20
  • 打赏
  • 举报
回复
时间是在程序里限定的,,,上面的方法我都试过了,,,不好用啊,,,
perfwell 2003-10-20
  • 打赏
  • 举报
回复
up
yangyangbeibei 2003-10-20
  • 打赏
  • 举报
回复
当然时间可以限定,,,比如从1999--2002

不过时间段是活动的,,,还可以写成2000--2002
yangyangbeibei 2003-10-20
  • 打赏
  • 举报
回复
表a的结构如下
ID year score
001 1999 1
001 2000 2
001 2001 3
002 1999 1
... ... .
怎样做才能将其转成如下格式
ID 1999 2000 2001 ...
001 1 2 3
002 1
... .
我想把提问改成这样,,,也可以做到吗???
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
create table a(id char(3),year int,score int)
insert a select '001', 1999, 1
union all select '001', 2000, 2
union all select '001', 2001, 3
union all select '002', 1999, 1


declare @sql varchar(8000)
set @sql = 'select distinct ID'
select @sql = @sql + ',IsNull(cast(sum(case [year] when '''+cast([year] as char(4))+''' then score end) as varchar(10)),'''') ['+cast([year] as char(4))+']'
from (select distinct [year] from a) aa

select @sql =@sql+' from a group by ID '
--print @sql
exec(@sql)


ID 1999 2000 2001
---- ---------- ---------- ----------
001 1 2 3
002 1
Wally_wu 2003-10-20
  • 打赏
  • 举报
回复
select distinct id, (
(case year when 1999 then score else 0 end) 1999,
(case zc when 2000 then score else 0 end) 2000,
(case zc when 2001 then score else 0 end) 2001
)
from table group by id
yujohny 2003-10-20
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select ID'
select @sql = @sql + ',sum(case [year] when '''+[year]+''' then score end) ['+[year]+']' from (select distinct [year] from 表a) as a
select @sql = @sql+' from 表a group by ID'
exec(@sql)
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
create table a(id char(3),year int,score int)
insert a select '001', 1999, 1
union all select '001', 2000, 2
union all select '001', 2001, 3
union all select '002', 1999, 1


declare @sql varchar(8000)
set @sql = 'select distinct ID'
select @sql = @sql + ',sum(case [year] when '''+cast([year] as char(4))+''' then score else 0 end) ['+cast([year] as char(4))+']'
from (select distinct [year] from a) aa

select @sql =@sql+' from a group by ID '
--print @sql
exec(@sql)


ID 1999 2000 2001
---- ----------- ----------- -----------
001 1 2 3
002 1 0 0

yujohny 2003-10-20
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select ID'
select @sql = @sql + ',sum(case [year] when '''+[year]+''' then score end) ['+[year]+']' from (select distinct [year] from 表a) as a
select @sql = @sql+' from 表a group by ID'
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
TRY:
declare @sql varchar(8000)
set @sql = 'select ID'
select @sql = @sql + ',(case [year] when '''+[year]+''' then score else 0 end) ['+[year]+']'
from (select distinct [year] from 你的表) a

select @sql =@sql+' from 你的表 group by ID'

exec(@sql)

22,206

社区成员

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

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