Get a sum of hours worked in a week

Posted on 2011-04-29
Last Modified: 2012-06-21
Hi all,
I'm not sure why, but I can't seem to get my head around the SQL for this one.  What I need to do is to get a sum of hours worked in a week where the work week is Saturday to Friday.  There may be no entry for any given day, and the hours are broken down into regular and overtime hours.  So, without the constraint of grouping hours by work week the SQL looks something like

SELECT Name, SUM(RegularHours + OvertimeHours)
FROM Hours

Thanks for your help.
Question by:edc
    LVL 9

    Expert Comment

    select name sum(isnull(RegularHours, 0) + isnull(OvertimeHours))
    from hours
    group by name

    You'll need the where clause in here to specify the week.

    BTW - this most likely is not the 'legal' way to do things.  Check up with your HR/Payroll departments.

    LVL 1

    Author Comment

    Hi sshah254.
    Thanks for your reply.  I appreciate you taking the time to share your knowledge with me.  The primary hurdle I am facing is actually how to group these results into weeks.  No worries about the legality of this.  It's a small out-of-band app for some folks to manage their own time.

    Thanks again.
    LVL 51

    Expert Comment

    i could not get what you are asking for...
    please post a sample data and a result that you are looking for...
    LVL 23

    Expert Comment

    by:Rajkumar Gs
    Do you mean something like this ?
    create table #table
    	date	datetime,
    	mark	int
    insert into #table
    select getdate(), 100 union all
    select getdate()-3, 11 union all
    select getdate()-8, 22 union all
    select getdate() -15, 22
    -- Main Query
    SELECT sum(mark) as [Total]       ,
    Week = DateAdd(day, -1 * datepart(dw, CONVERT(varchar,date,101)),    CONVERT(varchar,date,101))  FROM #table 
    GROUP BY DateAdd(day, -1 * datepart(dw, CONVERT(varchar,date,101)), CONVERT(varchar,date,101)) 
    ORDER BY [Week] DESC 
    drop table #table

    Open in new window

    LVL 1

    Author Comment

    Hi RajkumarGS,
    Sure.  Let's say John Smith has the following hours worked:

    Sat 4/16: 0 hrs / 0 overtime
    Sun 4/17: 0 hrs / 0 overtime
    Mon 4/18: 8 hrs / 0 overtime
    Tues 4/19: 5 hrs / 0 overtime
    Wed 4/20: 8 hrs / 3 hrs overtime
    Thur 4/21: 8 hrs / 1 hrs overtime
    Fri 4/22: 3 hrs / 0 overtime

    Sat 4/23: 0 hrs / 0 overtime
    Sun 4/24: 5 hrs / 0 overtime
    Mon 4/25: 8 hrs / 0 overtime
    Tues 4/26: 8 hrs / 2 overtime
    Wed 4/27: 8 hrs / 0 hrs overtime
    Thur 4/28: 8 hrs / 0 hrs overtime
    Fri 4/29: 8 hrs / 0 overtime

    I want to group week 1 (4/16 to 4/22) regular and overtime hours into a single number.  In this case 36 hours total, and week 2 (4/23 to 4/29) hours into a second number.  In this case 47 hours.

    So when I submit the query the results would be:
    4/22/2011 | 36
    4/29/2011 | 47
    LVL 51

    Accepted Solution

    you mean this
    create table hours
    	name	varchar(10),
    	date	date,
    	hrs	int,
    	overtime	int
    --delete from hours;
    insert into hours
    select 'Hain', getdate()-8, 2,2 union all
    select 'Hain', getdate()-7, 3,0 union all
    select 'Hain', getdate()-6, 5,0 union all
    select 'Hain', getdate()-5, 6,3 union all
    select 'Hain', getdate()-4, 7,1 union all
    select 'Hain', getdate()-3, 4,0 union all
    select 'Hain', getdate()-2, 5,0 union all
    select 'Hain', getdate()-1, 4,0 union all
    select 'Hain', getdate()-0, 0,0 union all
    select 'Kurt', getdate()-8, 4,1 union all
    select 'Kurt', getdate()-7, 5,0 union all
    select 'Kurt', getdate()-6, 3,0 union all
    select 'Kurt', getdate()-5, 6,2 union all
    select 'Kurt', getdate()-4, 7,2 union all
    select 'Kurt', getdate()-3, 5,0 union all
    select 'Kurt', getdate()-2, 6,0 union all
    select 'Kurt', getdate()-1, 8,2 union all
    select 'Kurt', getdate()-0, 0,0
    -- Main Query
    select name, DATEPART(week,date) week, SUM(hrs)+sum(overtime) totalHrs from hours
    group by name, datepart(week,date)
    name	week	totalHrs
    Hain	17	12
    Hain	18	30
    Kurt	17	13
    Kurt	18	38

    Open in new window

    LVL 1

    Author Closing Comment

    Yes, that will work nicely.  Thank you.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
    Viewers will learn how the fundamental information of how to create a table.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now