日期:2014-05-18  浏览次数:20732 次

工资查询SQL问题
姓名  工资 奖金 id 年度 月份
张1 100 300 1 2012 1
张2 200 400 2 2012 1
张3 200 500 3 2012 1
张4 200 600 4 2012 1
张1 200 1400 12 2012 2
张2 200 1500 13 2012 2
张3 200 1600 14 2012 2
张4 200 1700 15 2012 2

生成表2怎么实现
xh 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 总计 一季度 二季度 三季度 四季度
张1 300 1600 1900 1900
张2 600 1700 2300 2300
张3 700 1800 2500 2500
张4 800 1900 2700 2700


------解决方案--------------------
SQL code
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (姓名 nvarchar(4),工资 int,奖金 int,id int,年度 int,月份 int)
insert into [TB]
select '张1',100,300,1,2012,1 union all
select '张2',200,400,2,2012,1 union all
select '张3',200,500,3,2012,1 union all
select '张4',200,600,4,2012,1 union all
select '张1',200,1400,12,2012,2 union all
select '张2',200,1500,13,2012,2 union all
select '张3',200,1600,14,2012,2 union all
select '张4',200,1700,15,2012,2

select * from [TB]


select 姓名 as 姓名 ,
  max(case 月份 when 1 then 工资+奖金 else 0 end) '1月',
  max(case 月份 when 2 then 工资+奖金 else 0 end) '2月',
  max(case 月份 when 3 then 工资+奖金 else 0 end) '3月',
  max(case 月份 when 4 then 工资+奖金 else 0 end) '4月',
  max(case 月份 when 5 then 工资+奖金 else 0 end) '5月',
  max(case 月份 when 6 then 工资+奖金 else 0 end) '6月',
  max(case 月份 when 7 then 工资+奖金 else 0 end) '7月',
  max(case 月份 when 8 then 工资+奖金 else 0 end) '8月',
  max(case 月份 when 9 then 工资+奖金 else 0 end) '9月',
  max(case 月份 when 10 then 工资+奖金 else 0 end) '10月',
  max(case 月份 when 11 then 工资+奖金 else 0 end) '11月',
  max(case 月份 when 12 then 工资+奖金 else 0 end) '12月',
SUM(工资+奖金) AS 'SUM',
SUM(CASE WHEN 月份 between 1 and 3 THEN 工资+奖金 ELSE 0 END) '1季度',
SUM(CASE WHEN 月份 BETWEEN 4 AND 6 THEN 工资+奖金 ELSE 0 END) '2季度',
SUM(CASE WHEN 月份 BETWEEN 7 AND 9 THEN 工资+奖金 ELSE 0 END) '3季度',
SUM(CASE WHEN 月份 BETWEEN 10 AND 12 THEN 工资+奖金 ELSE 0 END) '4季度'
from tb
group by 姓名


/*
姓名   1月          2月          3月          4月          5月          6月          7月          8月          9月          10月         11月         12月         SUM         1季度         2季度         3季度         4季度
---- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
张1   400         1600        0           0           0           0           0           0           0           0           0           0           2000        2000        0           0           0
张2   600         1700        0           0           0           0           0           0           0           0           0           0           2300        2300        0           0           0
张3   700         1800        0           0           0           0           0           0           0           0           0           0           2500        2500        0           0           0
张4   800         1900        0           0           0           0           0           0           0           0           0           0           2700        2700        0           0           0

(4 行受影响)

*/

------解决方案--------------------
探讨
非常感谢 OrchidCat 您的帮助
1、想再问一个问题 就是把上面查询的结果保存到一个表中.
2、我在网页ASP中要用到这个表 是直接生成另个表 还是有什么办法让SQL自动生成

------解决方案--------------------
declare @sql varchar(8000)
set @sql = 'select [name]'
select @sql = @sql + ' , max(case [月份] when ''' + [月份] + ''' then score else 0 end) [' + [月份] + ']'
from (select distinct [月份] from t_XXX) as a
set @sql = @sql +SUM(工资+奖金) AS 'SUM',
SUM(CASE WHEN 月份 between 1 and 3 THEN 工资+奖金 ELSE 0 END) '1季度',
SUM(CASE WHEN 月份 BETWEEN 4 AND 6 THEN 工资+奖金 ELSE 0 END) '2季度',
SUM(CASE WHEN 月份 BETWEEN 7 AND 9 THEN 工资+奖金 ELSE 0 END) '3季度',
SUM(CASE WHEN 月份 BET