MYSQL - start and end time of overlapping periods - mysql

I have a table with several events and their starting and ending date/time.
Below I have a sample data with 4 events which overlaps simultaneosly in different periods of time.
For each period they overlap I should apply a multiplying factor to its duration.
What I've got so far:
I am using MySQL as a database;
I have a table with spatial index on the start/end date/time which I use with MBRIntersects to find overlaps among the events. The method works pretty fine, and I got rid of those many ifs and joins.
Thus, I can get the intersection start/end time of each event individually. But that doesn't help much.
I need to find the start/end time of the overlapping periods, so I can group them and apply the maximun factor in that range.
Maybe I am taking the wrong approuch.
What I cannot do so far: Find the start/end times of the overlapping periods. In the example shown I need to get intervals like so:
1 | 08:00 | 08:15
2 | 08:15 | 09:30
3 | 09:30 | 10:00
4 | 10:00 | 10:15
5 | 10:15 | 10:30
6 | 10:30 | 11:00

Related

Calculating "TotalshiftTiming - (Break01 + Lunch +Break02)

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

How to store previous 30 days and next 30 days data?

Suppose I have 100 items whose price vary daily. Say today is 11 sep 2017, I want to store price for these 100 items for last 30 days and next 30 days say from 11 Aug 2017 to 11 Oct 2017. If I opt for every date as a column name then I will have 100 rows ( 1 row per Item) and each row will have 60 column ( each column will display data for that day price ). Now after today ends, I have to delete column corresponding to 11 Aug and add column corresponding to 12 Oct. Should I use Cron for this purpose.
If you choose rational database then it is trivial task, you just need to created a table that link product price with date.
One for example:
| productId | day | price |
| 1 | 11-09-2017 | 50.25 |
| 2 | 11-09-2017 | 12.50 |
NO! 60 rolling rows in a table, not 60 rolling columns.
It is easy to INSERT and DELETE a row each day. It is messy to do the same with columns.
The table would have (at least): (date, item_id, price)

SQL lite query for get the records in which current time range exist between range of time slots

The requirement is to get the time slot from sql table with the help of current time . Here is the table below . The problem arises when i am trying to get the time slot that surpasses 24th hours for example like the Night shift . if my current time is 21:00 i am unable to get the time slot .
query made so far
SELECT
*,CURRENT_TIME
FROM
tbl_shift
WHERE CURRENT_TIME BETWEEN shift_start
AND shift_end
shift_id shift_name shift_start shift_end
-------- ---------- ----------- -----------
1 Morning 09:00:00 13:00:00
2 Evening 13:00:00 16:15:00
3 Night 16:01:00 09:00:00

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.

Find n occurrences within a date range that is within a larger date range

In MySQL, I have a table that records user "actions" (an action can be any number of things: a click, a purchase, etc). The table looks like so, where action_id is unique:
+--------------------------------------------+
| table_actions |
+--------------------------------------------+
| action_id | user_id | date_created |
+-----------+---------+----------------------+
| 1 | 321 | 2011-01-21 06:00:00 |
+-----------+---------+----------------------+
| 2 | 123 | 2011-02-21 06:00:00 |
+-----------+---------+----------------------+
| 3 | 456 | 2011-03-21 06:00:00 |
+-----------+---------+----------------------+
| 4 | 654 | 2011-04-21 06:00:00 |
+--------------------------------------------+
I want to find the number of distinct user_ids that took three actions no more than one year apart within a 30 month date range. My first try was this:
select ta.user_id
from table_actions ta
where ta.date_created > DATE_SUB(now(),interval 30 month)
group by ta.user_id
having COUNT(ta.action_id) > 2 AND DATEDIFF(MAX(ca.created_at),MIN(ca.created_at)) > 364;
This runs just fine, but errors in only checking the difference between a users first and last action. If user's first and last action are 30 months apart, but somewhere in that span of time they took 3 actions in 3 days, they would not be counted by my query.
My research found a few posts on date ranges, but none that looked for a certain number of occurrences within a range within a range.
I'm assuming this could involve a loop or row numbering of some kind, but I've quickly reached the limits of my knowledge.
Update 1:
If a real world example would help:
Jane takes action in March 2010, March 2011, April 2011 and December 2011.
John takes action in January 2010, January 2011, February 2011 and March 2012.
Each have take 3 or more actions in the past 30 months, but only Jane has taken 3 or more actions that fell within a 12 month period inside the last 30 months. I'm looking for a query that returns all the Janes.
Where I'm getting stuck is accounting for the fact that a user can take any number of actions in the past 30 months. If they were limited to only 3 actions, this would be simple. But, I don't know how to take a user who has taken 5 actions in the past 30 months and check if 3 of those actions fell in a year long period.
Hopefully that helps clarify. Thanks everyone.