Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Calculating employees weekly hours

Author  Topic 

ninel
Posting Yak Master

141 Posts

Posted - 2005-06-02 : 17:05:08
The pay period begins on a sunday and ends on a saturday. Some employees work on the weekends, some don't.

How can I calculate by week how many hours an employee has worked?

Date EmpNumber Hours
20050522(sun) 123 7.5
20050523(mon) 123 8
20050524(tues) 123 8.5
20050523(mon) 246 9
20050524(tues) 246 6.5
20050526(thur) 246 7.5
20050529(sun) 345 8
20050530(mon) 345 7
20050531(tues) 345 6.75

I need the outcome to look like this:
PayPeriodWeek EmpNumber Hours
20050522 123 24
20050522 246 23
20050529 345 21.75

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-06-02 : 18:28:05
With a little work, im sure you can make this query much better, but this should get you going.


declare @timesheet table ([date] datetime, EmpNumber int, Hours decimal(4,2))
insert into @timesheet
select '20050522',123, '7.5' union
select '20050523',123, '8' union
select '20050524',123, '8.5' union
select '20050522',246, '9' union
select '20050523',246, '6.5' union
select '20050524',246, '7.5' union
select '20050522',345, '8' union
select '20050523',345, '7' union
select '20050524',345, '6.75'

select d.payPeriod,
d.empNumber,
sum(d.hours) as totalHours
from (
select empNumber,
[date] - (datepart(dw,[date])-1) as payPeriod,
datepart(ww,[date]) + 1 - datepart(ww,dateadd(dd,-(datepart(dd,[date])-1),[date])) as weekOfMonth,
hours
from @timesheet) as d
group by d.empNumber, d.payPeriod
Go to Top of Page
   

- Advertisement -