Calculating "TotalshiftTiming - (Break01 + Lunch +Break02) - mysql

I have a table that displays shift start time, break 01 start time, break 01 end time, lunch start time, lunch end time, break 02 start time, break 02 end time,
shift end time.
Here I wanted to calculate everything in minutes.
Time = TotalShiftTime - ((break01Start - break01endtime) + (lunchstart - lunchend) + (break02start - break02end))
I want to calculate this in minutes on daily basis. I want to calculate this with respect to start date and end date which i wanted to give as input.
It has to be like
[(endDate - startDate) * Time]
If my endDate is 07/03/2018 and startDate is 04/03/2018 I want it to be [3 * Time].
PS: I don't have a column for startDate and endDate. I want to get cumulative minutes when I select system date.
How can I achieve this?
Shiftstart | Brk01start | Brk01end | LunchStart | LunchEnd | Brk02Start | Brk02end | Shiftend
8:00:00 | 11:15:00 | 11:45:00 | 13:00:00 | 13:30:00 | 15:15:00 | 15:30:00 | 17:00:00

Related

Get End Time by Adding Number of Minutes in Start Time Keeping in mind the working hours

I have database table in which I have daily working hours of office.
What I need is to get End Time by Adding Number of minutes in Start Time keeping in mind the office hours. The working hours are from 09:00 to 17:30
I will be thankful if some one could help me to write query in mysql so that I could calculate end time.
my Sample Table is
+------------------+------------------+
| starttime | endtime |
+------------------+------------------+
| 2017-01-01 00:00 | 2017-01-01 00:00 |
| 2017-01-02 09:00 | 2017-01-02 17:30 |
| 2017-01-03 09:00 | 2017-01-03 17:30 |
| 2017-01-04 09:00 | 2017-01-04 17:30 |
| 2017-01-05 09:00 | 2017-01-05 17:30 |
| 2017-01-06 09:00 | 2017-01-06 17:30 |
| 2017-01-07 09:00 | 2017-01-07 14:30 |
| 2017-01-08 00:00 | 2017-01-08 00:00 |
| 2017-01-09 09:00 | 2017-01-09 17:30 |
+------------------+------------------+
Input time : 2017-01-02 16:52
adding minutes: 300
required time : 2017-01-03 12:22
If I understand clearly, you want:
SELECT CONCAT(FLOOR((540 + t.time)/60),'h ', MOD(540 + t.time, 60),'m') as HOURS
FROM table t;
Using DATE_ADD is what you want
SELECT DATE_ADD(starttime, INTERVAL 300 MINUTE) as endtime;
EDIT:
Okay, I think i understand you know. The code below could probably be condensed, however, I left it verbose for readability.
SELECT CASE WHEN
DATE_ADD(TIMESTAMP(starttime), INTERVAL 300 MINUTE)
BETWEEN TIMESTAMP(DATE(starttime),'09:00')
AND TIMESTAMP(DATE(starttime),'17:30')
THEN
## starttime plus 5 hours, is still in range of current business window
DATE_ADD(starttime, INTERVAL 300 MINUTE)
WHEN TIMESTAMP(starttime) > TIMESTAMP(DATE(starttime),'17:30') THEN
## startime falls after business hours, add it to the following day at 09:00
DATE_ADD(TIMESTAMP(DATE(starttime),'09:00'),
INTERVAL (1440 /*24 hours (i.e. next day)*/ + 300 ) MINUTE
)
ELSE
## if the starttime falls within business hours, but extends into the next business day,
## calculate the difference up to 17:30, add to the following day, after 09:00
DATE_ADD(TIMESTAMP(DATE(starttime),'09:00'),
INTERVAL (
1440 + /*24 hours (i.e. next day)*/
300 - /* standard working day */
ABS(
TIMESTAMPDIFF(MINUTE, TIMESTAMP(DATE(starttime),'17:30'), starttime)
) /* less yesterday's minutes */
) MINUTE
)
END AS endtime
;

Mysql: get hour between selected time

I have a table like this:
id | date | time from | time to
1 | 2015-09-19 | 20:00 | 04:00
2 | 2015-09-19 | 10:00 | 23:00
3 | 2015-09-19 | 22:00 | 10:00
4 | 2015-09-20 | 10:00 | 16:00
For each row, I need hour between 22:00 and 06:00
Then first row has 6, second row has 1, third row has 8, fourth row has nothing.
Yes, it is possible. Just use some logic:
If time to is lower than time from then just use the amount of hours between midnight and time from but maximum 2 hours, and add the minimum between 6 hours (the max limit) and time to.
In time from is lower than time to then
-- if time from is greater than 6 add 0, else add time from
-- if time to is lower than 22 add 0, else add difference between 24 and time to.
SELECT
*,
CASE
WHEN time_to < time_from
THEN
TIME_TO_SEC (
LEAST(TIMEDIFF(TIME('24:00'), time_from), TIME('02:00')) +
LEAST(time_to, TIME('06:00'))
) / 60 / 60
ELSE
TIME_TO_SEC (
CASE WHEN time_from > TIME('06:00') THEN TIME('00:00') ELSE time_from END +
CASE WHEN time_to < TIME('22:00') THEN TIME('00:00') ELSE TIMEDIFF(TIME('24:00'), time_to) END
) / 60 / 60
END new_time_to
FROM demo;
Here is the example: http://sqlfiddle.com/#!9/78571/36/0

Select distinct and get sum of timestamp differences

I don't know if this is possible, but it'd be really awesome. I have a table of sign-ins for people who are logging time on different projects and I need to compile a report of time logged for each project for a given time period.
My table looks something like this:
id | project | time_in | time_out | break
----------------------------------------------------------------
1 | 1 | 2014-12-07 05:00:00 | 2014-12-07 10:00:00 | 30
2 | 2 | 2014-12-07 06:00:00 | 2014-12-07 13:00:00 | 15
3 | 1 | 2014-12-07 14:00:00 | 2014-12-07 18:00:00 | 0
4 | 3 | 2014-12-07 08:30:00 | 2014-12-07 18:45:00 | 75
5 | 2 | 2014-12-07 12:00:00 | 2014-12-07 16:30:00 | 0
What I'd like to be able to do is get a report of the time logged for each project given a date range, i.e. the total time, probably in seconds, logged for each project.
time_in and time_out are fields of type TIMESTAMP; break is an integer representing the number of minutes the person was on break. I need to get the sum of time_out - time_in - break for each project, e.g. for December 7:
project | time
---------------
1 | 34200
2 | 40500
3 | 34200
This is all I have so far:
SELECT DISTINCT
`project`
FROM `sign_ins`
WHERE
`time_in` >= '2014-12-07 00:00:00' AND
`time_out` <= '2014-12-08 00:00:00';
I appreciate your help on this, SO community. You guys are so brilliant.
You can get the difference in seconds by converting the date/time values to Unix time stamps. Then, just aggregate the differences using sum():
SELECT project,
SUM(UNIX_TIMESTAMP(time_out) - UNIX_TIMESTAMP(time_in) - (break * 60)) as DiffSecs
FROM `sign_ins`
WHERE `time_in` >= '2014-12-07 00:00:00' AND
`time_out` <= '2014-12-08 00:00:00'
GROUP BY project;

Check how many entries are in table by date

Im trying to create a specific query.
E.g
i have table with events:
id | start | end
-----------------------------------------
1 | 10-08-2013 12:00 | 10-08-2013 14:00
2 | 10-08-2013 12:00 | 10-08-2013 14:00
3 | 10-08-2013 15:00 | 10-08-2013 16:00
And i want to insert a new event( start: 13:00, end: 15:30 ) and before that i want to check by query how many events are on the same time. In this case results should be 3: beacause 2 events are in start time and one is on the end time.
Thanks.
The correct logic for overlap is:
where new_startDate <= end and
new_endDate >= start
give this a try,
SELECT COUNT(DISTINCT ID) totalCOunt
FROM tableName
WHERE new_startDate BETWEEN start AND end
OR
new_endDate BETWEEN start AND end
where new_startDate and new_endDate are the new dates of the event.

mysql counting in a particular time period

I am a newbie in mysql, and i encounter a difficult problem for me.
I have a table storing some room booking information
room_id
booking_start_time
booking_end_time
eg.
room_id | booking_start_time | booking_end_time<br>
1 | 2012-01-01 09:00 | 2012-01-01 11:00
1 | 2012-01-02 09:00 | 2012-01-02 10:00
1 | 2012-01-03 08:00 | 2012-01-03 10:00
2 | 2012-01-01 08:00 | 2012-01-01 10:00
3 | 2012-01-01 08:00 | 2012-01-01 09:00
And i have to do a report to extract the utilization of a room in a month, that is the number of hours used in a particular period of time
and the format is like the following
room 1| room 2
09:00 -10:00|
10:00 -11:00|
My question is that how can count the record, if the booking time is across a number of hours like from 9:00 -11:00, but i have to mark 1 hour in the period 09:00 to 10:00 and another 1 hour in the period 10:00 to 11:00.
I really struggle about it.
Your replies are really appreciated.
Thanks a lot.
Take a look at the MySQL Date Time Functions. Particularly, TIME_DIFF().
In your case, do a TIME_DIFF() for your *booking_time* columns. Combined with a GROUP BY for room_id, you could easily SUM() these for each room in a month.
I recognize that this answer isn't comprehensive. I'm encouraging you to learn these functions.