Select where datetime start greater than and end less than - mysql

I have a table with start and end field with values such as:
start | end
2021-09-24 17:00:00 | 2021-10-01 08:00:00
I have gone foggy headed and can't figure out why this statement results in nothing:
SELECT *
FROM `oncall`
WHERE `start` >= '2021-10-01 02:00:00'
AND `end` <= '2021-10-01 08:00:00'
although the date range in my example row contains the above range.
I am trying to find any row that overlaps the start and end of the values in the database.

Looking at the entry in your database and the query you're using, it seems like you're actually looking for a date interval overlap query i.e. a query that checks if [2021-10-01 02:00:00, 2021-10-01 08:00:00) overlaps [2021-09-24 17:00:00, 2021-10-01 08:00:00) somehow.
SELECT *
FROM `oncall`
WHERE #d2 > `start` AND `end` > #d1
-- replace #d1 and #d2 with actual values
Note that this query works for all kinds of overlap. For example if you have this pair of dates in your database:
2021-10-11 | 2021-10-15
then all of these input pairs will match:
2021-10-01 | 2021-10-12
2021-10-14 | 2021-10-20
2021-10-01 | 2021-10-20
2021-10-11 | 2021-10-14

Related

MySQL MAX foreach day

I have the following table with measured values:
datetime | water | air | conductivity | ...
2021-07-17 16:44:39 | 13,9 | 18,6 | 357 | ...
I am currently querying the values ​​for a period of time:
SELECT * FROM kn1 WHERE datetime > TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 month)) ORDER BY datetime
Now I don't want to get every value, just the highest value of each day.
In general, I can query the highest value through
SELECT MAX(water) FROM kn1 WHERE datetime > TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 month)) ORDER BY datetime
But this way I get the highest value for the entire period. But I would like to get the highest value for each day in the period.
How can I do this?
Thanks in advance
Use GROUP BY:
SELECT DATE(datetime), MAX(water)
FROM kn1
WHERE datetime > CURDATE() - INTERVAL 1 month)
GROUP BY DATE(datetime);
Note that I modified the WHERE clause to simplify it. Presumably, you don't date about the current time, just the date, so this uses the current date. Second, there is no need for TIMESTAMP, and third, I find interval arithmetic to be simpler without additional functions.

Return rows between two dates

I have ItemsRent table,
ID | ParentID | SubID | StartDate | EndDate |
--------------------------------------------------------------------
1 | 100 | 102 | 2014-09-09 17:40:00 | 2014-11-09 17:40:00 |
2 | 70 | 73 | 2014-08-09 14:20:00 | 2014-12-09 13:40:00 |
The dates are in sql format.
My input dates are:
InputStartDate: 2014-09-09 18:00:00
InputEndDate: 2014-10-09 13:47:00
And i want to return the best row only of the dates are between two dates. So for example:
Lets call StartDate as S, and EndDate as E.
And input dates will be InputStartDate as IS, and InputEndDate as IE.
S E
|----------------|
IS IE
|XXXXXXX--------|
Any suggestions ?
This query will produce a result matching your illustration. It will find all rows where any time at all was spent between InputStartDate and InputEndDate, and output a modified date range that is clamped by InputStartDate and InputEndDate.
SELECT ID, ParentId, SubId,
MAX( InputStartDate, StartDate ) AS Date_Start,
MIN( InputEndDate, EndDate ) AS Date_End
FROM `itemsRent`
WHERE InputStartDate <= EndDate AND InputEndDate >= StartDate
Normally, I would say to use the BETWEEN operator, but since you are storing both the start and end dates in the table, this would get more complicated than it needs to be. If you assume that the start date being stored is before the end date, you only need to perform two checks.
SELECT * FROM `itemsRent` WHERE `StartDate` > 1410285600 AND `EndDate` < 1410356820
This verifies that the start date of the item takes place after the specified start date. The issue with this is that it does not check if it takes place before the end date. Instead of explicitly writing this check, you can make sure of this by checking that the item's end date takes place before the specified end date.
NOTE: Might cause issues if the start date does not occur before the end date. If this is a possibility, then you will need to explicitly write these checks. This would be a good case in which to use the BETWEEN operation.
Why not just pull out the max end date in the table as the high date?
SELECT *
FROM itemsRent
WHERE (InputStartDate BETWEEN startdate AND end date)
AND (InputEndDate BETWEEN startdate AND enddate);
Your query is currently looking for rows whose EndDate is earlier than your provided StartDate or after your provided EndDate. I don't think that is what you want.
If you want rows that both StartDate and EndDate are between your provided dates (lets call them your-start-date and your-end-date), your query should be something like:
SELECT * FROM `itemsRent` WHERE `StartDate` > your-start-date AND `StartDate` < your-end-date AND `EndDate` > your-start-date AND `EndDate` < your-send-date

MySQL date_add behaviour

I've written some SQL to give me a range of dates between two times like so:
select date_add(x.min_date, interval ((t500.id-1) * 30) minute) period
from (
select '2013-08-05T23:00' as min_date, '2013-08-06T01:00' as max_date
) x,
t500
where date_add(x.min_date, interval ((t500.id-1) * 30) minute) <= x.max_date);
Where T500 is a trivial table with column id of 1 to 500 I use for simulating a loop.
Now I expect this to return:
2013-08-05 23:00:00
2013-08-05 23:30:00
2013-08-06 00:00:00
2013-08-06 00:30:00
2013-08-06 01:00:00
and finish there. But instead it carries on until 2013-08-06 23:30:00. I tried different max dates and it always returns dates to the end of the day. Could someone explain what's happening and how to make it stop when I want?
First thing that comes to mind would be casting your date strings into a date format instead of a string for example:
cast('2013-08-05T23:00' as smalldatetime)

Summing value differences across date range

My table is defined as follows:
DATETIME ENERGY VALUE
01/01/2013 00:00:00 1000
...
01/01/2013 08:00:00 2000
...
06/30/2013 00:00:00 10000
...
06/30/2013 08:00:00 12000
I need to calculate total energy value between: start hour of 00:00:00 and end hour 08:00:00 in a single day then sum these values for date range between 01/01/2013 and 06/30/2013.
Any idea will be appreciated.
Pseudo code:
SELECT SUM(DAILYENERGYSUM)
FROM
(SELECT SUM(ENGERGY) as DAILYENERGYSUM
FROM TABLE
WHERE DATEPART(DATETIME) >= '1/1/2013' and DATEPART(DATETIME) <= '6/30/2013' and TIMEPART(DATETIME) >= 0 and TIMEPART(DATETIME) <= 8
GROUP BY DATEPART(DATETIME)) AS DAYSUMS

Mysql DateTime compare

Hi I have a table of the following 2 records:
descript | start | end
test 1 | 2011-07-18 14:30:00 | 2011-07-18 17:00:00
test 2 | 2011-07-18 00:00:00 | 2011-07-19 00:00:00
When I tried to do a select, I can't seems to retrieve the 2nd result (test 2) which seems like clearly it is dated 19th of July.
SELECT * FROM event WHERE start >= "2011-07-18 00:00:00" AND end <= "2011-07-18 23:59:59";
Would appreciate any advise.
"2011-07-19 00:00:00" is MORE than "2011-07-18 23:59:59"
By your condition it should be less, so your query does not match test 2.
Your SQL query should be:
SELECT * FROM event
WHERE start >= "2011-07-18 00:00:00"
AND end <= "2011-07-19 00:00:00";
You can just do this :)
SELECT * FROM event
WHERE start BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
AND end BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
This results in the times in between the range you specified for start AND end
you just need to exclude column end, like
SELECT * FROM event
WHERE start>="2011-07-18 00:00:00" AND start<="2011-07-18 23:59:59";