slot
start_time
end_time
t1
06:00:00
09:00:00
t2
09:00:00
11:00:00
t3
11:00:00
12:00:00
This is sample table.
Given start_time and end_time is 08:30:00 - 09:15:00
How to write sql query for find slot available or not.
Already try below code is not working
SELECT
*
FROM
TABLE
WHERE ((start_time <= '08:30:00'
AND end_time >= '09:15:00')
or((start_time < '08:30:00'
AND end_time > '08:30:00')
and(start_time < '09:15:00'
AND end_time > '09:15:00')))
Excepted Result
slot
start_time
end_time
t1
06:00:00
09:00:00
t2
09:00:00
11:00:00
scenario 2
SAMPLE TABLE
slot
start_time
end_time
t1
06:00:00
09:00:00
t2
10:00:00
11:00:00
t3
11:00:00
12:00:00
If table like this expected result will get null . Why second table t1 start from 6 - 9 and next slot t2 start from 10 - 11. our given time is 8.30 to 9.15.
You can write something like this:
SELECT *
FROM my_table
WHERE start_time <= '08:30:00' AND end_time >= '09:15:00'
Related
MySQL version 8.0
I want to calculate time difference between two datetime column.
And get rows where duration >= 12:00:00.
which I would normally do:
select id
, start_time
, end_time
, timediff(end_time, start_time) as duration
from table;
which I would get something like this:
id start_time end_time duration
0 1 2020-06-01 01:00:00 2020-06-01 14:00:00 13:00:00
1 2 2020-06-01 01:00:00 2020-06-01 18:00:00 17:00:00
2 3 2020-06-01 19:00:00 2020-06-02 10:00:00 15:00:00
3 4 2020-06-02 04:00:00 2020-06-02 16:00:00 12:00:00
For duration column I don't want times between 00:00:00 ~ 04:00:00 to be added towards the duration. So for the first row duration = 10:00:00 since 01:00:00~14:00:00 = 10:00:00, ignoring times between 00:00:00 ~ 04:00:00
same for second row we substract 3 hours from duration.
so my desired output would be:
id start_time end_time duration
0 1 2020-06-01 01:00:00 2020-06-01 14:00:00 10:00:00
1 2 2020-06-01 01:00:00 2020-06-01 18:00:00 14:00:00
2 3 2020-06-01 19:00:00 2020-06-02 10:00:00 11:00:00
3 4 2020-06-02 04:00:00 2020-06-02 16:00:00 12:00:00
There are lots of rows where times include minutes and seconds too.
Thanks in advance!
I've grabbed all rows where duration >= 12:00:00.
Then separated data into 4 regions depending on their start_time.
a_region = 00~04
b_region = 04~12
c_region = 12~16
d_region = 16~24
For a_region I've subtracted 04:00:00 - start_time which is time we should compensate to duration in a_region.
compensation = 04:00:00 - start_time
compensated_time = duration - compensation.
For b_region it needs no compensation if it has passed 00~04 it means it already passed duration = 12:00:00.
For c_region,
compensation = 16:00:00 - start_time
compensated_time = duration - compensation
For d_region since we've grabbed duration >= 12:00:00
it will pass all of 00~04 therefore
compensated_time = duration - 04:00:00.
I solved it using Python but above is the logic I've used.
One option uses greatest():
select id
, start_time
, end_time
, timediff(
greatest(,
end_time,
date_format(end_time, '%Y-%m-%d 04:00:00')
),
greatest(
start_time,
date_format(start_time, '%Y-%m-%d 04:00:00')
)
) as duration
from table;
MariaDB version 10.2.21
I have a table that looks like:
user_id item_id start_time end_time
1 412374 2349 2020-01-01 04:01:44 2020-01-01 04:01:51
2 271274 2519 2020-01-01 04:02:41 2020-01-01 04:03:33
3 271274 2519 2020-01-01 04:05:37 2020-01-01 04:06:14
4 349729 6979 2020-01-01 04:10:44 2020-01-01 04:10:49
5 79011 2785 2020-01-01 04:54:38 2020-01-01 04:55:06
6 68486 5395 2020-01-01 05:39:23 2020-01-01 05:40:03
7 68486 5395 2020-01-01 05:55:23 2020-01-01 05:59:03
Within 10 minutes => row 2's end_time and row 3's start_time is within 10 minutes. Even though row 1 end_time and row 2 start_time are within 10 minutes I do not want it since they are different user.
row 6,7: user_id is the same and bought same item however row 6 end_time ~ row 7 start_time is over 10 minutes therefore I do not want them.
desired table after filter:
user_id item_id start_time end_time
1 271274 2519 2020-01-01 04:02:41 2020-01-01 04:03:33
2 271274 2519 2020-01-01 04:05:37 2020-01-01 04:06:14
Thanks in advance!
You can use an EXISTS query to check for a transaction on the same user_id and item_id within a 10 minute period:
SELECT *
FROM sales s1
WHERE EXISTS (SELECT *
FROM sales s2
WHERE s2.id != s1.id
AND s2.user_id = s1.user_id
AND s2.item_id = s1.item_id
AND (s2.start_time BETWEEN s1.end_time AND s1.end_time + INTERVAL 10 MINUTE
OR s1.start_time BETWEEN s2.end_time AND s2.end_time + INTERVAL 10 MINUTE
)
)
ORDER BY id
Output:
id user_id item_id start_time end_time
2 271274 2519 2020-01-01 04:02:41 2020-01-01 04:03:33
3 271274 2519 2020-01-01 04:05:37 2020-01-01 04:06:14
Demo on dbfiddle
Slightly different form of output side by side rather than list:
SELECT t1.*, t2.*
FROM tbl t1
JOIN tbl t2
ON t1.id!=t2.id
AND t1.user_id = t2.user_id
AND t1.item_id = t2.item_id
AND (t1.start_time BETWEEN t2.start_time - INTERVAL 10 MINUTES AND t2.end_time + INTERVAL 10 MINUTES
OR t1.end_time BETWEEN t2.start_time - INTERVAL 10 MINUTES AND t2.end_time + INTERVAL 10 MINUTES)
My table have fields that represent starting and ending working period as datetime.
I need to find related entries that match a total of 14hours min over a sliding period of 24 hours.
I think window function will (maybe) save me, but MariadDB (i use) doesn't implement yet Range time intervals in window function.
here is some example data:
id starting_hour ending_hour
-- ------------------- -------------------
1 2018-09-02 06:00:00 2018-09-02 08:30:00
2 2018-09-03 08:30:00 2018-09-03 10:00:00
4 2018-09-03 11:00:00 2018-09-03 15:00:00
5 2018-09-02 15:30:00 2018-09-02 16:00:00
6 2018-09-02 16:15:00 2018-09-02 17:00:00
7 2018-09-20 00:00:00 2018-09-20 08:00:00
8 2018-09-19 10:00:00 2018-09-19 12:00:00
9 2018-09-19 12:00:00 2018-09-19 16:00:00
10 2018-10-08 12:00:00 2018-10-08 14:00:00
11 2018-10-29 09:00:00 2018-10-29 10:00:00
So how to find rows where in a 24 hours window their sum a more or equal to 14 hours.
thanks
Edit:
SELECT
id,
starting_hour,
ending_hour,
TIMEDIFF (ending_hour, starting_hour) AS duree,
(
SELECT SUM(TIMEDIFF(LEAST(ending_hour, DATE_ADD(a.starting_hour, INTERVAL 24 HOUR)), starting_hour)) / 10000
FROM `table` b
WHERE b.starting_hour BETWEEN a.starting_hour AND DATE_ADD(a.starting_hour, INTERVAL 24 HOUR)
) AS duration
FROM
`table` a
HAVING duration >= 14
ORDER BY starting_hour ASC
;
This returns Id 8 but i want the whole period. (eg: Id 8, Id 9 and Id 7)
EDIT2:
The expected results are ranges of working time where they are in a window of 24 hours and where their sum are more or equal to 14 hours.
EDIT 3:
In fact under MySQL 8 this seems to work.
SELECT * FROM (
SELECT
*,
SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(hs.`ending_hour`, hs.`starting_hour`))) OVER (ORDER BY hs.starting_hour RANGE BETWEEN INTERVAL '12' HOUR PRECEDING AND INTERVAL '12' HOUR following)) AS tot
FROM
table hs
WHERE hs.`starting_hour` > DATE_SUB(NOW(), INTERVAL 50 DAY) AND hs.`ending_hour` <= NOW()
ORDER BY hs.`starting_hour` ASC
) t1
HAVING tot >= '14:00:00'
;
Is there a way to do it under MariaDB 10.2 without window function ? Or without window range function ?
Need help for MySQL query for retrieving record of class schedule that is conflict in the given time.
Ex.
SchedID StartTime EndTime
1 09:00:00 13:00:00
2 08:30:00 10:00:00
3 11:00:00 15:00:00
4 07:30:00 08:30:00
5 11:30:00 13:00:00
I would like to retrieve the list that is conflict in this given time
Start Time = 09:00:00
End Time = 11:00:00
The record will yield following result:
SchedID StartTime EndTime
1 09:00:00 13:00:00
2 08:30:00 10:00:00
3 11:00:00 15:00:00
Thank you.
Try this
SELECT DISTINCT a.schedid, a.starttime, a.endtime
FROM
tbl_name a, tbl_name b
WHERE ((b.starttime > a.starttime AND b.starttime < a.endtime)
OR (b.endtime > a.starttime AND b.endtime < a.endtime))
ORDER BY schedid
i just found my answer here Determine Whether Two Date Ranges Overlap
Heres my query:
SELECT * FROM tableName WHERE (start_time < givenEndTime)
AND (end_time > givenStartTime);
This works perfectly fine for me.
I need a query similar to this one:
SELECT *
FROM myTable
WHERE
DATE_FORMAT(date,'%Y-%m-%d') BETWEEN <1> AND <2>
Where <1> is the date of the first day of the month and <2> is the date of the day 5 days earlier. For example, if the current date is 2013-03-14, <1> should be 2013-03-01 and <2> 2013-03-09.
How can I do this?
Query:
SQLFIDDLEExample
SELECT
DATE_FORMAT(NOW(),'%Y-%m-%d') d1,
DATE_FORMAT(NOW(),'%Y-%m-01') d2,
DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -5 DAY),'%Y-%m-%d') d3
Result:
| D1 | D2 | D3 |
----------------------------------------
| 2013-03-14 | 2013-03-01 | 2013-03-09 |
So:
SELECT *
FROM myTable
WHERE DATE_FORMAT(date,'%Y-%m-%d')
BETWEEN DATE_FORMAT(NOW(),'%Y-%m-01')
AND DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -5 DAY),'%Y-%m-%d')
This query probabbly don't gonna use INDEX because of formatting column with function DATE_FORMAT(date,'%Y-%m-%d')
But if your column date datatype is Datetime When you can make your query to:
SELECT *
FROM myTable
WHERE date >= DATE_FORMAT(CURDATE(),'%Y-%m-01')
AND date <= DATE_ADD(CURDATE(), INTERVAL -5 DAY)
SELECT *
FROM myTable
WHERE
DATE_FORMAT(date,'%Y-%m-%d') BETWEEN
concat(extract(year_month FROM CURDATE()),"01")
AND
from_days(to_days(CURDATE())-5)