我怎样才能从opid = 30和31的表中对列进行求和,其中workid相同

时间:2014-01-14 08:16:11

标签: sql-server

我有这张桌子

job id  op id   hours_actu   hours_est  hours_plan      work_id
10195   31      0            3.12       3.12            2011
10195   30      2.25         0.67       0.67            3042
10195   31      4.5          3.34       3.34            3042
10195   15      2.25         0.67       0.67            3042
10195   16      4.5          3.34       3.34            3042

我需要进行查询才能获得这样的数据

job id    30,31_actu 30,31_est 30,31_plan 15,16_actu 15,16_est  15,16_plan  work_id
10195       0         3.12      3.12                                             2011
10195      6.75     4.01        4.01         6.75         4.01             4.01      3042 

1 个答案:

答案 0 :(得分:0)

试试这个......

 declare  @jobmaster as table(jobid int,opid int,hours_actu float,hours_est float,
                       hours_plan float,work_id int)
insert into @jobmaster values
(10195,31,0,3.12,3.12,2011)


insert into @jobmaster values
(10195,30,2.25,0.67,0.67,3042)

insert into @jobmaster values
(10195,31,4.5,3.34,3.34,3042)

select jobid,sum(hours_actu) hours_actu,sum(hours_est) hours_est,sum(hours_plan) hours_plan,work_id from @jobmaster
group by work_id,jobid