I'm trying to figure out how to select the latest DateTime value in a table and all previous data that came with it in a 30 minute window using MySQL.
eg table:
DateAndTime
---------
09:00:00
08:59:50
08:59:40
08:59:30
08:59:20
08:59:10
08:59:00
08:58:50
08:59:40
...
...
08:30:00
I am selecting max time as such:
SELECT MAX(`DateAndTime`) AS "DateAndTime"
FROM TableA;
I have been trying the INTERVAL() function, but I can't seem to get that to return any other rows other than the max time.
What I tried:
SELECT MAX(`DateAndTime`) AS "DateAndTime"
FROM TableA;
AND `DateAndTime` - INTERVAL 30 MINUTE;
We can use your query as subquery in the WHERE clause:
SELECT DateAndTime
FROM tableA
WHERE DateAndTime >=
(SELECT MAX(DateAndTime) - INTERVAL 30 MINUTE
FROM tableA);
If we want to select further columns, we will just add them in the main query.
If we want to make sure the result will be sorted by date, we will add an ORDER BY clause:
SELECT DateAndTime
FROM tableA
WHERE DateAndTime >=
(SELECT MAX(DateAndTime) - INTERVAL 30 MINUTE
FROM tableA)
ORDER BY DateAndTime;
It looks like you'll need a subquery because you need an aggregate function to find the latest timestamp, then to use that value to return the rows you need. Try this:
SELECT DateAndTime
FROM TableA a
WHERE DATE_ADD(a.DateAndTime, INTERVAL 30 MINUTE) >=
(SELECT MAX(DateAndTime) FROM TableA)
Related
I have id, date,time(doenst have date) columns, and I am just wondering how to select rows where id repeated at least two other times(repeated three times) in last hour..
enter image description here
use GROUP BY query to get duplicate ID
select *
from 'table'
where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR)
group by id
having count(id) >= 2
select * from 'table' where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR) group by id having count(id) >= 2
Will select records within last hour, group them by id and filter which group have more than 1 records
I want to get the count of records between two date-time entries.
I have a column in my table named created_date with the datetime data type.
I want to select rows which were created between 2017-01-10 and 2017-01-30
I have written the following query but it doesn't seem to be inclusive
SELECT* FROM table WHERE created_date BETWEEN '2017-01-10' AND '2017-01-30'
The issue you are having has to do with that the date literal 2017-01-31 represents that date at midnight. To get around this, phrase your query as follows:
SELECT * FROM table WHERE created_date >= '2017-01-10' AND created_date < '2017-01-31';
This says to take any date on or after the very start of 2017-01-10 and before the start of 2017-01-31. This implies including the entire day 2017-01-30.
For get count of records between two date you can try below query
SELECT COUNT(*)
FROM tableName
WHERE date(created_date) >= '2017-01-10' AND date(created_date) <= '2017-01-30'
Try This One
SELECT COUNT(1)
FROM tableName
WHERE Cast(created_date as date) Between Cast('2017-01-10' as date) AND Cast('2017-01-30' as date)
I took this column (datatype = timestamp) image from a MySQL result window from a query I executed. I have 14 records in this query result and what I'm trying to do is DELETE those records that have a timestamp that is 2 minutes later than the first set of rows (7). Any suggestions/direction on how to accomplish this would be appreciated. Hope this makes sense. Thanks.
Hope this help
SELECT t.*
FROM table t
WHERE t.time > DATE_ADD( SELECT MIN(t2.time)
FROM table t2
, INTERVAL 2 MINUTE)
In MySQL, delete records having timestamps greater than or equal to two minutes later than the latest timestamp of the first seven records:
DELETE FROM table t1
WHERE t1.ApplicationDate >= DATE_ADD(
(SELECT MAX(t1.ApplicationDate) FROM (SELECT ApplicationDate FROM table LIMIT 7) t1)
, INTERVAL 2 MINUTE
)
Explanation:
First we need to get the first 7 records:
SELECT ApplicationDate FROM table LIMIT 7
From that we need to get the MAX (latest) timestamp:
SELECT MAX(t1.ApplicationDate) FROM (...)
We can then feed that as the first parameter into the DATE_ADD function, and 2 minutes as the second parameter:
DATE_ADD((...), INTERVAL 2 MINUTE)
This gives us the target timestamp to use as a condition for deletion of records:
DELETE FROM table t1 WHERE t1.ApplicationDate >= (...)
I have a mysql database with vehicles records. I need a fast query that will return the newest records of those records that were updated within the last 4 minutes. For example vehicle "A" may be updated several times a minute so it will appear many times within the last 4min. Same with vehicle B C etc. I need only the most recent entries for each vehicle within a 4 min window. I have tried like this
SELECT *
FROM yourtable AS a
WHERE a.ts =
(SELECT MAX(ts)
FROM yourtable AS b
WHERE b.ts > NOW() - INTERVAL 5 MINUTE
AND b.name = a.name)
but it takes too long to produce results >10seconds.
You don't need the self-join.
select max(ts), name from Table1
where ts > NOW() - INTERVAL 5 MINUTE
group by name
To get all the rows for the latest updates and not only the name and timestamp:
SELECT t.*
FROM
TableX AS t
JOIN
( SELECT name
, MAX(ts) AS maxts
FROM TableX
WHERE ts > NOW() - INTERVAL 4 MINUTE
GROUP BY name
) AS grp
ON grp.name = t.name
AND grp.maxts = t.ts
You'll need at least an index on the timestamp column for this query.
I need to do a query and join with all days of the year but in my db there isn't a calendar table.
After google-ing I found generate_series() in PostgreSQL. Does MySQL have anything similar?
My actual table has something like:
date qty
1-1-11 3
1-1-11 4
4-1-11 2
6-1-11 5
But my query has to return:
1-1-11 7
2-1-11 0
3-1-11 0
4-1-11 2
and so on ..
This is how I do it. It creates a range of dates from 2011-01-01 to 2011-12-31:
select
date_format(
adddate('2011-1-1', #num:=#num+1),
'%Y-%m-%d'
) date
from
any_table,
(select #num:=-1) num
limit
365
-- use limit 366 for leap years if you're putting this in production
The only requirement is that the number of rows in any_table should be greater or equal to the size of the needed range (>= 365 rows in this example). You will most likely use this as a subquery of your whole query, so in your case any_table can be one of the tables you use in that query.
Enhanced version of solution from #Karolis that ensures it works for any year (including leap years):
select date from (
select
date_format(
adddate('2011-1-1', #num:=#num+1),
'%Y-%m-%d'
) date
from
any_table,
(select #num:=-1) num
limit
366
) as dt
where year(date)=2011
I was looking to this solution but without the "hardcoded" date, and I came-up with this one valid for the current year(helped from this answers).
Please note the
where year(date)=2011
is not needed as the select already filter the date. Also this way, it does not matter which table(at least as stated before the table has at least 366 rows) is been used, as date is "calculated" on runtime.
select date from (
select
date_format(
adddate(MAKEDATE(year(now()),1), #num:=#num+1),
'%Y-%m-%d'
) date
from
your_table,
(select #num:=-1) num
limit
366 ) as dt
Just in case someone is looking for generate_series() to generate a series of dates or ints as a temp table in MySQL.
With MySQL8 (MySQL version 8.0.27) you can do something like this to simulate:
WITH RECURSIVE nrows(date) AS (
SELECT MAKEDATE(2021,333) UNION ALL
SELECT DATE_ADD(date,INTERVAL 1 day) FROM nrows WHERE date<=CURRENT_DATE
)
SELECT date FROM nrows;
Result:
2021-11-29
2021-11-30
2021-12-01
2021-12-02
2021-12-03
2021-12-04
2021-12-05
2021-12-06