假设我有一张这样的桌子叫做小时:
employee, payperiod, hours
greg, 1/31/2010, 12
greg, 1/31/2010, 15
mark, 1/31/2010, 10
mark, 1/31/2010, 11
greg, 2/28/2010, 12
greg, 2/28/2010, 15
greg, 2/28/2010, 4
如何查找给定员工和工资期的本年迄今的工作时间?
employee, payperiod, hours_YTD
greg, 1/31/2010, 27
mark, 1/31/2010, 21
greg, 2/28/2010, 58
mark, 2/28/2010, 21 (this row is a nice to have, I can live without it if it's too tricky)
到目前为止,我已经试过了,但总的来说似乎是关闭:
select employee,payperiod,sum(hours) from hours h1
join hours h2 on h2.payperiod<=h1.payperiod and h2.payperiod>=1/1/2010
and h1.employee=h2.employee
group by h1.employee, h1.payperiod