如何将纵向结构的数据转换为横向结构的数据?求一条SQL语句

chuanyi 2006-01-20 03:45:25
奖惩 次数 金额 年度
大功 2 360 2002
小功 17 1020 2002
小功 3 360 2002
大功 9 1620 2003
嘉獎 2 80 2003
警告 25 -500 2003
警告 1 -40 2003
大功 1 180 2004
嘉獎 12 240 2004
嘉獎 2 80 2004
警告 18 -360 2004
警告 1 -40 2004
大功 3 540 2005
小功 7 420 2005
小過 11 -660 2005

转化为:
年度 小功 大功 嘉奖 记过 大过 警告 总金额
2002
2003
2004
2005

最后的金额是年度金额合计,即sum()
这条sql语句怎么写啊??如果实在写不了,请给我一个大概的解决思路
...全文
181 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2006-01-20
  • 打赏
  • 举报
回复
create table t(奖惩 varchar(8),次数 int,金额 int,年度 char(4))
insert t
select '大功',2,360,'2002' union all
select '小功',17,1020,'2002' union all
select '小功',3,360,'2002' union all
select '大功',9,1620,'2003' union all
select '嘉獎',2,80,'2003' union all
select '警告',25,-500,'2003' union all
select '警告',1,-40,'2003' union all
select '大功',1,180,'2004' union all
select '嘉獎',12,240,'2004' union all
select '嘉獎',2,80,'2004' union all
select '警告',18,-360,'2004' union all
select '警告',1,-40,'2004' union all
select '大功',3,540,'2005' union all
select '小功',7,420,'2005' union all
select '小過',11,-660,'2005'
go

declare @s varchar(8000)
set @s='select isnull(年度,''合计'') as [年度]'
select @s=@s+',['+奖惩+']=sum(case 奖惩 when '''+奖惩+''' then 金额 else 0 end)'+
',['+奖惩+'次数]=sum(case 奖惩 when '''+奖惩+''' then 1 else 0 end)'
from t group by 奖惩
set @s=@s+',合计=sum(金额) from t group by 年度 with rollup'
exec(@s)
/*

年度 大功 大功次数 嘉獎 嘉獎次数 警告 警告次数 小功 小功次数 小過 小過次数 合计
---- ----------- -------- --------- --------- -------- --------- ------ -------- ---------------------
2002 360 1 0 0 0 0 1380 2 0 0 1740
2003 1620 1 80 1 -540 2 0 0 0 0 1160
2004 180 1 320 2 -400 2 0 0 0 0 100
2005 540 1 0 0 0 0 420 1 -660 1 300
合计 2700 4 400 3 -940 4 1800 3 -660 1 3300


*/

drop table t
-狙击手- 2006-01-20
  • 打赏
  • 举报
回复
create table t(奖惩 varchar(8),次数 int,金额 int,年度 char(4))
insert t
select '大功',2,360,'2002' union all
select '小功',17,1020,'2002' union all
select '小功',3,360,'2002' union all
select '大功',9,1620,'2003' union all
select '嘉獎',2,80,'2003' union all
select '警告',25,-500,'2003' union all
select '警告',1,-40,'2003' union all
select '大功',1,180,'2004' union all
select '嘉獎',12,240,'2004' union all
select '嘉獎',2,80,'2004' union all
select '警告',18,-360,'2004' union all
select '警告',1,-40,'2004' union all
select '大功',3,540,'2005' union all
select '小功',7,420,'2005' union all
select '小過',11,-660,'2005'
go

declare @s varchar(8000)
set @s='select isnull(年度,''合计'') as [年度]'
select @s=@s+',['+奖惩+']=sum(case 奖惩 when '''+奖惩+''' then 金额 else 0 end)'
from t group by 奖惩
set @s=@s+',合计=sum(金额) from t group by 年度 with rollup'
exec(@s)
/*

年度 大功 嘉獎 警告 小功 小過 合计
---- ----------- ----------- ----------- ----------- ----------- -----------
2002 360 0 0 1380 0 1740
2003 1620 80 -540 0 0 1160
2004 180 320 -400 0 0 100
2005 540 0 0 420 -660 300
合计 2700 400 -940 1800 -660 3300
*/

drop table t
-狙击手- 2006-01-20
  • 打赏
  • 举报
回复
支持楼上
mislrb 2006-01-20
  • 打赏
  • 举报
回复
--给个金额的

declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',['+奖惩+']=isnull(sum(case when 奖惩='''+奖惩+''' then 金额 end),0)' from yourtable group by 奖惩 order by 奖惩
set @sql='select 年度'+@sql+',总金额=sum(金额) from yourtable group by 年度 order by 年度'
exec(@sql)

34,837

社区成员

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

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