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

给出第几周,如16,17周,如何查询求得如下格式?
周表:
工号     姓   名     加班状况(天)   周次           考核分数      
  001     张三                 2             16                   8
  002     王五                 1             16                   6
  003     李四                 1             16                   7
  001     张三                 3             17                   10
  002     王五                 2             17                   8
  003     李四                 1             17                   7

然后给出参数周次(16,17),如何查询得出以下格式?请高手帮忙,给出答案,马上给分....

工号   姓名   第16周考核分数     第17周考核分数       两周平均分数       两周加班数
001     张三               8                         10                               9                             5
002     王五               6                           8                               7                             3
003     李四               7                           7                               7                             2


------解决方案--------------------

create table test(工号 varchar(10),姓名 varchar(10),[加班状况(天)] int,周次 int,考核分数 int)
insert test select '001 ', '张三 ',2,16,8
union all select '002 ', '王五 ',1,16,6
union all select '003 ', '李四 ',1,16,7
union all select '001 ', '张三 ',3,17,10
union all select '002 ', '王五 ',2,17,8
union all select '003 ', '李四 ',1,17,7

select 工号,姓名,[第16周考核分数],[第17周考核分数],[两周平均分数]=([第16周考核分数]+[第17周考核分数])/2,
[两周加班数] from
(
select 工号,姓名,[第16周考核分数]=sum(case 周次 when 16 then 考核分数 else 0 end),
[第17周考核分数]=sum(case 周次 when 17 then 考核分数 else 0 end),[两周加班数]=sum([加班状况(天)]) from test
group by 工号,姓名
)a
order by a.工号
------解决方案--------------------
select isnull(A.工号,B.工号),isnull(A.姓名,B.姓名),A.考核分数 第16周考核分数,
B.考核