In mysql db I have a field named time_utilize with type time which store total time spend in a day and that table contain records for every mon开发者_Python百科th. Now I want to find total time spend by a user in each week of a selected month. I searched for that but I found an excellent query to find total time spend in whole month which is:
SELECT Sec_to_Time(Sum(Time_to_Sec(time_utilize))) FROM attendance WHERE staff_id = 'sajid'
Now who can I find the sum of total time utilize in each week of a month?
Assuming that the attendance
table has a date column attendance_date
, the below query may give you total time utilized per week in the month:
SELECT WEEK(`attendance_date`) `attendance_week`,
SEC_TO_TIME(SUM(TIME_TO_SEC(`time_utilize`))) `attendance_time_utilized`
FROM `attendance`
GROUP BY `attendance_week`;
In the above query, the attendance_week
is calculated as the week of the year, between 1 and 52.
Another form of output might be to show weeks as 1,2,3,4. For it, you may try this query:
SELECT (`attendance_week` - `min_week` + 1) `att_week`, `attendance_time_utilized`
FROM (
SELECT WEEK(`ATT`.`attendance_date`) `attendance_week`,
SEC_TO_TIME(SUM(TIME_TO_SEC(`ATT`.`time_utilize`))) `attendance_time_utilized`,
`T1`.`min_week`
FROM `attendance` `ATT`
INNER JOIN (SELECT MIN(WEEK(`attendance_date`)) `min_week` FROM `attendance`) T1
GROUP BY `attendance_week`
) T2;
Hope the above queries help you with the desired results. Please change the table and column names as per your schema.
精彩评论