The table below contains records of shifts which have taken place. The start and end fields are the start and end timestamps of those shifts. I'm looking to build a query that will extract the total hours per month that the shifts cover.
Example table:
ID Start End
1 2018-10-23 10:30:00 2018-10-23 11:45:00
2 2018-10-22 22:00:00 2018-10-22 23:00:00
3 2018-11-22 22:00:00 2018-11-22 23:00:00
The ideal output would read:
Month Hours
10 2:15
11 1:00
I've got some of the elements worked out, using a SUM(timediff(end,start)) and GROUP BY, but havn't managed to get something good out!
Thanks!
Here you go:
select
month(start) as month,
time_format(sec_to_time(
sum(timestampdiff(second, start, end))
), '%H:%i') as hours,
sum(timestampdiff(second, start, end)) as seconds
from shift
group by month(start)
Result:
month hours seconds
----- ----- -------
10 02:15 8,100
11 01:00 3,600
Note: I added the extra column seconds in case you want to use this numeric value to do some extra processing.
Related
I am working in mySQL and I currently have a count of total orders by day, but I would like to add Saturday and Sunday orders to Monday then remove Saturday and Sunday values. I have done some research on this but I cannot seem to find anything similar to what I am trying to do.
My current data table looks like this:
Date | Daily Count
8-6-2020 25
8-7-2020 82
8-8-2020 24
8-9-2020 33
8-10-2020 18
8-11-2020 10
8-12-2020 25
8-13-2020 15
I need it to look something like this:
Date | Daily Count
8-6-2020 25
8-7-2020 82
8-10-2020 75
8-11-2020 10
8-12-2020 25
8-13-2020 15
In this one the Daily counts for the 8th and 9th are added to the 10th, then removed, because they are weekend days. Thank you in advance for your help!
Consider using a case expression to adjust the date:
select
case weekday(date)
when 5 then date + interval 2 day
when 6 then date + interval 1 day
else date
end as new_date,
sum(daily_count) as daily_count
from mytable
group by new_date
This is the query for calculating the hour_diff which i added in input
SELECT t.START_TIME, t.END_TIME, (TIME_TO_SEC(t.END_TIME) - TIME_TO_SEC(t.START_TIME))/(60*60) as hour_diff FROM table1 as t ;
INPUT
start time --- end time ---- hour_diff
10:20:00 10:40:00 .33 hour
11:00:00 11:15:00 .25 hour
11:00:00 12:20:00 .33 hours
11:00:00 13:00:00 2 hours
14:00:00 17:00:00 3 hours
14:20:00 18:00:00 3.66 hours
OUTPUT
I what that output should be categorized as
0th hour - 1st hour --> x1 rows
1st hour - 2nd hour ---> x2 rows
like this:
0-1 ----> 3
1-2 ----> 1
2-3 ----> 1
3-4 ----> 1
4-5 ----> 0
Can someone suggest this I have start time and end time in my table, hours i now how to calculate that's why i included it in query but i don't know how to categorize it in hour basis.
You could try grouping by hour_diff like this and counting the number of results:
SELECT truncate((TIME_TO_SEC(t.END_TIME) - TIME_TO_SEC(t.START_TIME))/(60*60),0) as hour_diff, count(*)
FROM table1 as t
group by truncate((TIME_TO_SEC(t.END_TIME) - TIME_TO_SEC(t.START_TIME))/(60*60),0);
I am creating a clock-in time system and so far I have been able to get user clock in time for today and user clock in time for the current week.
The final step is to get user current time for the current pay period.
I have created a list of pay period start & end dates in Excel.
Whenever you use a function like Excel WEEKNUM() or MySQL YEARWEEK(), these functions come with an additional option parameter.
The links below show the differences between these modes in a table.
Excel WEEKNUM() table reference
MySQL YEARWEEK() table reference
My question is, if we do payroll biweekly, which mode do I set in Excel WEEKNUM() that corresponds to MySQL YEARWEEK()?
Attached spreadsheet clock.logic.xlsx
Thank you for any help.
At first the good news: The Excel ISOWEEKNUM function corresponds to the MySQL WEEKOFYEAR which is WEEK(date,3). So determining ISO week numbers is possible.
But all other WEEK modes are simply crap because the definition of the first week in year does not fit any logic used elsewhere. For example, take the simplest mode having Sunday as the first day of the week and the first week of the year is the week, the first day of the year falls in. This is what Excels WEEKNUM function returns with Return_type 1 or omitted. This should be MySQLs WEEK in modus 0 (0-53) or 2 (1-53). But what the heck?
SELECT WEEK('2008-01-01',0); -> 0
SELECT WEEK('2008-01-01',2); -> 52
So MySQL tells us, Tuesday, 2008-01-01, is in week 52 of 2007?
Really? Why?
Because the rule "Week 1 is the first week … with a Sunday in this year" is not fulfilled by MySQL. Instead it seems for MySQL the first week starts with the first Sunday in this year.
So except of the ISO week numbers, all other week numbers from MySQL are wrong. One could think: Let us take modus 0 and simply add 1 to the result. But that fails in 2012. Because there 2012-01-01 is Sunday and there MySQL gives week number 1 in modus 0 as well as in modus 2.
Examples:
Excel:
Date WEEKNUM ISOWEEKNUM
2008-01-01 1 1
2008-02-01 5 5
2008-02-03 6 5
2008-02-04 6 6
2008-12-31 53 1
2009-01-01 1 1
2009-02-01 6 5
2009-12-31 53 53
2012-01-01 1 52
2012-02-01 5 5
2012-12-31 53 1
2016-01-01 1 53
2016-02-01 6 5
2016-12-31 53 52
MySQL:
drop table if exists tmp;
create table tmp (d date);
insert into tmp (d) values
('2008-01-01'),
('2008-02-01'),
('2008-02-03'),
('2008-02-04'),
('2008-12-31'),
('2009-01-01'),
('2009-02-01'),
('2009-12-31'),
('2012-01-01'),
('2012-02-01'),
('2012-12-31'),
('2016-01-01'),
('2016-02-01'),
('2016-12-31');
select d as 'Date', week(d,0), week(d,3) from tmp;
Result:
Date week(d,0) week(d,3)
2008-01-01 0 1
2008-02-01 4 5
2008-02-03 5 5
2008-02-04 5 6
2008-12-31 52 1
2009-01-01 0 1
2009-02-01 5 5
2009-12-31 52 53
2012-01-01 1 52
2012-02-01 5 5
2012-12-31 53 1
2016-01-01 0 53
2016-02-01 5 5
2016-12-31 52 52
If you want to calculate hours in current pay period in Excel, given a two week pay period, then I'd suggest that you don't need week numbers at all (in fact that overcomplicates the calculation, especially at the start or end of the year)
If you have dates in A2:A100 and hours worked on those dates in B2:B100, and a list of pay period start dates in Z2:Z10 then you can get hours in current pay period with this formula
=SUMIF(A2:A100,">="&LOOKUP(TODAY(),Z2:Z10),B2:B100)
I imagine your actual setup is more complicated, but some variation on the above can probably still be used
I have a MySQL table similar to this:
item | order | start date | end date
------------------------------------------
1 1 2015-09-15 2015-09-20
2 1 2015-09-15 2015-09-20
1 2 2015-09-20 2015-09-25
2 2 2015-09-20 2015-09-25
What I want to do is execute a query that will check if any end-dates are within 7 days of a future start date, and return the result. Does anyone know how this could be done?
EDIT: Should be more specific I suppose - the start date and end date of an order (say in this case order 2 from the example table) can be within 7 days of each other. I want to check if order 1's end date is within 7 days of order 2's start date. Sorry if that wasn't clear before.
You can use datediff function.
select * from table_name
where
start_date > curdate()
and datediff(end_date,start_date) between 0 and 7
In the project I'm working on right now the system stores employees' timetables in the table with the following structure:
employee_id | mon_h_s | mon_m_s | mon_h_e | mon_s_e | tue_h_s | tue_m_s | etc.
------------------------------------------------------------------------------
1 06 00 14 30 06 00 ...
2 18 30 07 00 21 00 ...
where:
mon_h_s - monday hours start
mon_m_s - monday minutes start
mon_h_e - monday hours end
mon_m_e - monday minutes end
tue_... - tuesday...
Every day of the week has 4 fields: hours start, minutes start, hours end, minutes end.
So, from the table above we can see that:
employee with the id 1 works from 06:00 to 14:30 on Monday
employee with the id 2 works from 18:30 to 07:00 on Monday (basically, between Monday and Tuesday, at night)
The problem is that I'm not sure how to create a SQL query which takes into account everything including time overlapping (at night time). For example, we need to find an employee who works at 6am (06:00) on Tuesday. In our case both employees (id 1 and id 2) would satisfy this criteria. Employee with the id 1 starts his work at 06:00 on Tuesday, and employee with the id 2 works until 07:00 Tuesday (starts on Monday though).
Any suggestions on how to solve this problem would be greatly appreciated.
Probably something like:
SELECT (1440 + ((mon_h_e*60)+mon_m_e) - ((mon_h_e*60)+mon_m_e)) % 1440
This will give you the time worked in minutes. Basically, it adds 1440 (minutes in a day, or 24h*60min/h) to the difference between end time and start time, and keep the rest (modulo) of 1440.
Now for the design part:
If you can, redesign your table. Your table need not have all days of the week in one row, that will make tallying of weekly times very tedious.
You should consider using real datetimes.
employee_id | entrytime | exittime
1 | 2011-10-31 06:00:00 | 2011-10-31 14:30:00
1 | 2011-11-01 06:00:00 | null
2 | 2011-10-31 18:30:00 | 2011-11-01 07:00:00
2 | 2011-11-01 21:00:00 | null
That way, you have:
Full access to all date and time functions in MySQL
Easy calculation of duration
Easy filtering on incomplete periods
There are four basic cases that you need o handle
A -> when time of lecture starts before given time
B -> when time of lecture starts after given time but falls within range of ending time
C -> when time of lecture starts within given time but ends after
D -> when time of lecture starts before given time and ends after given time
Now, this can be accomplished using simple OR conditions