如何實現下列數據表格式轉換﹖

金木水火土1968 2003-08-17 11:26:29

如下表(物理表)﹕

編號 姓名 月份 工資
---- ---- ------- ----
001 王三 2003-01 2200
001 王三 2003-02 2120
001 王三 2003-03 2300
001 王三 2003-04 2260
002 李四 2003-01 3200
002 李四 2003-02 3200
002 李四 2003-03 2900
002 李四 2003-04 3400
......


生成如下格式的表﹕

編號 姓名 1月 2月 3月 4月 工資合計
---- ---- ---- ---- ---- ---- --------
001 王三 2200 2120 2300 2260 8880
002 李四 3200 3200 2900 3400 12700
......


請教如何編寫SQL語句﹗ 謝﹗﹗﹗
...全文
36 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-08-18
  • 打赏
  • 举报
回复
select 編號,姓名,
sum(case when 月份 like '%01' then 工資 end) 1月,
sum(case when 月份 like '%02' then 工資 end) 2月,
sum(case when 月份 like '%03' then 工資 end) 3月,
sum(case when 月份 like '%04' then 工資 end) 4月,
sum(case when 月份 like '%05' then 工資 end) 5月,
sum(case when 月份 like '%06' then 工資 end) 6月,
sum(case when 月份 like '%07' then 工資 end) 7月,
sum(case when 月份 like '%08' then 工資 end) 8月,
sum(case when 月份 like '%09' then 工資 end) 9月,
sum(case when 月份 like '%10' then 工資 end) 10月,
sum(case when 月份 like '%11' then 工資 end) 11月,
sum(case when 月份 like '%12' then 工資 end) 12月,
sum(工資)工資合計
from 表 group by 編號,姓名
pengdali 2003-08-18
  • 打赏
  • 举报
回复
select 編號,姓名,
sum(case when 月份 like '%01' then 工資 end) 1月,
sum(case when 月份 like '%02' then 工資 end) 2月,
sum(case when 月份 like '%03' then 工資 end) 3月,
sum(case when 月份 like '%04' then 工資 end) 4月,
sum(case when 月份 like '%05' then 工資 end) 5月,
sum(case when 月份 like '%06' then 工資 end) 6月,
sum(case when 月份 like '%07' then 工資 end) 7月,
sum(case when 月份 like '%08' then 工資 end) 8月,
sum(case when 月份 like '%09' then 工資 end) 9月,
sum(case when 月份 like '%10' then 工資 end) 10月,
sum(case when 月份 like '%11' then 工資 end) 11月,
sum(case when 月份 like '%12' then 工資 end) 12月,
sum(工資)工資合計
from 表 group by 編號,姓名
stone100 2003-08-18
  • 打赏
  • 举报
回复
我也曾有过相似问题,请访问这里:

http://expert.csdn.net/Expert/topic/1964/1964358.xml?temp=.8975336
foxstarx 2003-08-17
  • 打赏
  • 举报
回复
应该在月份的两端加上中括号,否则报错。
weixiao51 2003-08-17
  • 打赏
  • 举报
回复
select A.编号 , B.姓名,1月,2月,3月,4月, 工资合计
from (select 编号, 姓名, sum(工资) as 工资合计 from table group by 编号, 姓名) A
inner join (select 编号, 工资 AS 1月 from table where 月份='2003-01') B on A.编号=B.编号
inner join (select 编号, 工资 AS 2月 from table where 月份='2003-02') B on A.编号=B.编号
inner join (select 编号, 工资 AS 3月 from table where 月份='2003-03') B on A.编号=B.编号
inner join (select 编号, 工资 AS 4月 from table where 月份='2003-04') B on A.编号=B.编号
sdhdy 2003-08-17
  • 打赏
  • 举报
回复
select 编号 , 姓名,1月,2月,3月,4月, 工资合计=1月+2月+3月+4月 from (select 编号 , 姓名,
sum(case when 月份='2003-01' then 工资 else 0 end) as 1月,
sum(case when 月份='2003-02' then 工资 else 0 end) as 2月,
sum(case when 月份='2003-03' then 工资 else 0 end) as 3月,
sum(case when 月份='2003-04' then 工资 else 0 end) as 4月
from tablename
group by 编号 , 姓名)a order by 编号 , 姓名
sdhdy 2003-08-17
  • 打赏
  • 举报
回复
select 编号 , 姓名,
sum(case when 月份='2003-01' then 工资 else 0 end) as 1月,
sum(case when 月份='2003-02' then 工资 else 0 end) as 2月,
sum(case when 月份='2003-03' then 工资 else 0 end) as 3月,
sum(case when 月份='2003-04' then 工资 else 0 end) as 4月,
sum(工资) as 工资合计
from tablename
group by 编号 , 姓名

22,209

社区成员

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

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