How to get data from date and time range in MySQL - mysql

MySQL Table: -
ID | From_DateTime | To_DateTime
1 2014-09-01 10:00:00 2014-09-10 22:00:00
Explanation:
I have added 2 columns in database table for Date and Time range. The data shown above means that from 01-09-2014 to 10-09-2014 is the date range and 10:00:00 to 22:00:00 is the time range.
Positive Scenario - Now I am passing 2014-09-05 15:00:00 in my query which comes into Date and Time both range. What I need is the ID from the query.
Negative Scenario - Now I am passing 2014-09-05 23:00:00 in my query which comes into Date comes into range but TIME is not in the specified range so I should get 0 result.
I have no idea about database queries and that is why I am posting it to here to get some help from the database experts.

Something like this should work. May need some tweaking.
SELECT ID FROM (
SELECT ID FROM <tablename>
WHERE DATE('<dateTimeValue>') BETWEEN DATE(From_DateTime) AND DATE(To_DateTime)
AND TIME('<dateTimeValue>') BETWEEN TIME(From_DateTime) AND TIME(To_DateTime)
UNION
SELECT 0 AS ID FROM DUAL
) AS a LIMIT 1

SELECT id FROM date_time
WHERE From_DateTime < '2014-09-05 15:00:00'
AND To_DateTime > '2014-09-05 15:00:00';
here date_time is table name...
Don't forgot to put date in single quotes

Related

How to get all rows where date is in a specific range at a given period?

How can I select all rows from a table where a date column is within a specific range of dates, at a given period (e.g. every 14 days)?
The table has a date column with most every date represented, possibly multiple times. The range is defined by a start date and an end date. The period is a number of days. For example:
Start: 2016-01-01 (friday)
End: 2016-12-31 (saturday)
period: 14 (days)
For the above, the query should return rows for every other Friday in 2016. That is, it should return the rows for the following dates:
2016-01-01
2016-01-15
2016-01-29
2016-02-12
2016-02-26
2016-03-11
2016-03-25
2016-04-08
2016-04-22
2016-05-06
2016-05-20
2016-06-03
2016-06-17
2016-07-01
2016-07-15
2016-07-29
2016-08-12
2016-08-26
2016-09-09
2016-09-23
2016-10-07
2016-10-21
2016-11-04
2016-11-18
2016-12-02
2016-12-16
2016-12-30
Currently, this is done in a stored procedure where a loop fills a temp table with the target dates, which is later joined on. However, I am trying to rewrite this code to step away from stored procedures.
What would be the best way to get the desired rows without using the stored procedure & a temp table? Keep in mind that (one of) the table(s) is quite large at around 1M records indexed on date, so any calculated values might impact the performance severely.
Alternatively, I could calculate all dates in the interval in PHP/RoR and use a massive IN clause, but hopefully there is a better solution.
Try this:
table_name1 is your table
date1 the date field
"2022-01-02" the start (twice included)
"2022-01-10" the end
3 the interval
SELECT date1
FROM table_name1
WHERE date1 BETWEEN "2022-01-02" AND "2022-01-10"
AND (DATE("2022-01-02") - date1) % 3 = 0;
Tested it with MySQL 5.6.

Intersect between two date intervals

I need to find intersect between to date intervals for example
I have one date
2015-01-01 and 2015-03-01
and second value
2015-01-01 and 2015-01-15
I wanna get results 15. So how many days of second date is included in first date range?
Any ideas how to do it with MySql?
In MySQL you can use the following SQL statement to get what you need:
SELECT DATEDIFF(LEAST('2015-03-01 23:59:59','2015-01-15 23:59:59'),GREATEST('2015-01-01 00:00:00','2015-01-01 00:00:00'))+1 AS days;
+------+
| days |
+------+
| 15 |
+------+
This will get the date difference in days using DATEDIFF
You can use CASE for condition like below:
SELECT (CASE
WHEN '2015-01-01'>= '2015-01-01' AND '2015-04-15'<='2015-03-01'
THEN DATEDIFF('2015-01-15','2015-01-01')+1
ELSE 'out of range'
END) AS days;
You can specify your own else condition and date range.

Get peak amount of active rows from all time

I have the following problem. I have a mysql table that has a startdate and and enddate. Each row is considered active between those dates. Some rows are no longer active, but have been active in the past. For example the following table:
id start end
1 2014-11-11 00:00:00 2015-01-31 23:59:59
2 2014-09-25 10:16:14 2015-06-01 23:59:59
3 2013-12-24 00:00:00 2014-12-01 23:59:59
4 2014-08-13 00:00:00 2016-01-31 23:59:59
5 2013-09-11 00:00:00 2014-09-10 23:59:59
My actual table has way more data than that. Now I need to know what the peak amount of concurrent active rows is without knowing when that peak actually occured. How would I do this in SQL? In the example 4 rows are active at the same time (1-4, not 5) in the time between 2014-11-11 and 2015-01-31 23:59:59. The actual peak time doesn't matter to me as much as the peak amount itself.
Thanks for your help
Find different timestamps of interrest using UNION ALL, count number of active tasks at these timestamps:
select ts, (select count(*) from tablename t2
where timestamps.ts between t2.start and t2.end) as count
from (select start as ts
from tablename
union all
select end
from tablename) as timestamps
order by count desc
limit 1
Finally order descending and pick the highest value only!
(From a non MySQL user, so some details may be wrong... Please comment if that's the case and I'll edit!)

group by month of unix timestamp field

I'm trying to get my code to output in the following format:
january 2012 - 34
february 2012 - 23
where 34 and 23 would be a count of the total rows that fall within that month that have the id_dealership of 7. I need this to output all data for every month that an assignment was ever made.
The assignments table structure is as follows:
id_dealer (int)
date_assigned (int)
I've tried this but it does not work at all:
SELECT MONTH(date_assigned), YEAR(date_assigned), COUNT(*)
FROM assignments
GROUP BY MONTH(date_assigned), YEAR(date_assigned)
SELECT
MONTH(FROM_UNIXTIME(date_assigned)),
YEAR(FROM_UNIXTIME(date_assigned)),
COUNT(*)
FROM assignments
GROUP BY
MONTH(FROM_UNIXTIME(date_assigned)),
YEAR(FROM_UNIXTIME(date_assigned))
Your date_assigned column should be of type DATE. AFAIK MONTH works on date columns
and if you want the month name from a DATE column use : MONTHNAME(date_assigned)
try this query
SELECT
MONTH(FROM_UNIXTIME(date_assigned)),
YEAR(FROM_UNIXTIME(date_assigned)),
COUNT(*)
FROM assignments
GROUP BY 1,2
For people who would like to output a DATETIME rather than a month/year combo, here's another way to solve the problem. The benefit of using DATETIME is that it can easily be plugged into data visualization libraries and tools.
SELECT
LAST_DAY(FROM_UNIXTIME(date_assigned)),
COUNT(*)
FROM assignments
GROUP BY 1
ORDER BY 1 DESC
The LAST_DAY() function returns the last day of the month for a given DATE or DATETIME value. If you'd rather grab the first day, you could select this instead: ADDDATE(LAST_DAY(SUBDATE(FROM_UNIXTIME(date_assigned), INTERVAL 1 MONTH)), 1). It adds a day to the last date then subtracts a month.
The 1 values are column position integers -- shorthand so we don't have to type LAST_DAY(FROM_UNIXTIME(date_assigned)) any more than we need to (they start at 1, not 0).
Example output:
|-------------------------------------------|------------------|
| LAST_DAY(FROM_UNIXTIME(date_assigned)) | COUNT(*) |
|-------------------------------------------|------------------|
| September 30, 2020, 12:00 AM | 34 |
|-------------------------------------------|------------------|
| August 31, 2020, 12:00 AM | 23 |
|-------------------------------------------|------------------|

Need to get records from MySQL database by date only from a datetime field

This seems rather simple but it has been giving me a headache. I have a column in my events table that is called 'date_time' and it's type is datetime.
I need to write a query that will get events by day.
Example of table
============================
| id | date_time |
============================
| 5 | 2009-03-27 14:16:00 |
============================
Now I need to get the event with the Id = 5, except I only have a date (unix timestamp) to work with. I have tried many things such as converting to mysql format and selecting between 2009-03-27 00:00:00 and 2009-03-28 00:00:00 but I couldn't get it to work.
What is the best way to do this?
Thanks muchly.
SELECT * FROM table
WHERE date_time BETWEEN '2009-03-27 00:00:00' AND '2009-03-27 23:59:59'
Should do it.
Alternatively, try this:
SELECT * FROM table WHERE DATE(date_time) = '2009-03-27'
The DATE() function extracts the date part of the datetime column.
select * from table WHERE date(date_time) = '2009-03-27' works for me
if you have an unix timestamp you could also do
select * from table WHERE UNIX_TIMESTAMP(date_time) = your_timestamp
BETWEEN isn't safe if timestamps include fractional seconds. This is always safe:
SELECT *
FROM table
WHERE date_time >= '2009-03-26 00:00:00' AND date_time < '2009-03-27 00:00:00'