Search code examples
sqldata-analysis

Finding Weekly Summary from a table


I am here to write a SQL statement for finding the weekly summary from a table. I had a table with following fields:

UIN, Date, Staff, work_hours

Now I would like to gather information how many hours has a staff worked in one week.


Solution

  • It's hard to tell from your question, but if you're looking for the total hours in one week by each employee, try the following:

    SELECT Staff, SUM(work_hours)
    FROM YourTable
    WHERE Date BETWEEN week_start_day AND week_end_day
    GROUP BY Staff
    

    Otherwise, if you're looking for a full report, summarized by week, you can try the following, which will give you each staff's weekly hours, aggregated by week:

    SELECT YEAR(Date), WEEK(Date), Staff, SUM(work_hours)
    FROM YourTable
    GROUP BY YEAR(Date), WEEK(Date), Staff
    ORDER BY YEAR(Date), WEEK(Date)