Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:02:48+00:00 2026-06-03T07:02:48+00:00

my sql server table named timesheet has 4 columns employee_id int task_start_date datetime task_end_date

  • 0

my sql server table named timesheet has 4 columns

employee_id int
task_start_date datetime
task_end_date datetime
hours_worked int

I need to derive week start date and week end date and total hours the employee has worked in that week. My resulting dataset should have 4 columns –
Week_Start_Date Week_End_Date Employee_Id Total_Hours_Charged

In other words, i need to get total hours charged by each employee for all the weeks from that table. Could anyone please help me write a query for that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:02:49+00:00Added an answer on June 3, 2026 at 7:02 am

    SQL Server 2005 and above

    Assumptions:

    1. Task start and end date values are always same.
    2. Week start and end dates are calculated based on the task_start_date.

    You can use the DATEPART function to find the day of the week for a given date and then use DATEADD function to provide the output from DATEPART as input to calculate the first and last days of the week.

    Click here to view the demo in SQL Fiddle.

    Script:

    The script calculates the week totals hours assuming that week begins on Sunday and ends on Saturday.

    CREATE TABLE timesheet
    (
            employee_id     int
      ,     task_start_date datetime
      ,     task_end_date   datetime
      ,     hours_worked    int
    );
    
    INSERT INTO timesheet
        (employee_id, task_start_date, task_end_date, hours_worked) 
        VALUES
          (1, '20120331', '20120331', 6),
          (1, '20120401', '20120401', 3),
          (1, '20120403', '20120403', 8),
          (1, '20120409', '20120409', 5),
          (1, '20120412', '20120412', 4),
          (2, '20120402', '20120402', 7),
          (2, '20120403', '20120403', 6),
          (3, '20120409', '20120409', 4),
          (1, '20120412', '20120412', 8);
    
    ;WITH empworkhours AS
    (
        SELECT  DATEADD(DAY
                    ,   -(DATEPART(dw, task_start_date) -1)
                    ,   task_start_date) AS week_start
            ,   DATEADD(DAY
                    ,   7 - (DATEPART(dw, task_start_date))
                    ,   task_start_date) AS week_end
            ,   employee_id
            ,   hours_worked
        FROM    timesheet
    )
    SELECT      week_start
            ,   week_end
            ,   employee_id
            ,   SUM(hours_worked) total_hrs_per_week
    FROM        empworkhours
    GROUP BY    week_start
            ,   week_end
            ,   employee_id;
    

    Output:

    WEEK_START      WEEK_END        EMPLOYEE_ID TOTAL_HRS_PER_WEEK
    --------------  --------------  ----------- -----------------
    March, 25 2012  March, 31 2012       1               6
    April, 01 2012  April, 07 2012       1              11
    April, 01 2012  April, 07 2012       2              13
    April, 08 2012  April, 14 2012       1              17
    April, 08 2012  April, 14 2012       3               4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have a table in SQL server named as [QueryResult] with columns of type
I have a SQL Server 2005 legacy database which has a table named user
i have a table named 'Words' in sql server with these columns: Id, Type,
I have a table in SQL Server 2005 database that has following columns: Id,ProductName,Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
I have a table named myTable on SQL Server 2008 R2 Express. I would
I have a constraint (named INVS_ITEM_LOCATIONS_PK ) in my SQL Server 2005 table. How
I have this table named sample with these values in MS Sql Server: ID
I have a server table which has a column named 'SN' in mysql, when
I have a table named t_Student in Microsoft SQL Server 2005 database. In that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.