I am trying to set up an event/scheduler to grab the last minutes worth of data every single minute of the day with phpmyadmin.
Some how it is missing data out, it picks up some of the data but misses some out.
Can anyone advise on how to tweak my queries or another way how to do this ?
Here are the queries/events I'm using . --
GET ALL DATA FROM TRANDATA WITHIN THIS DATE RANGE -
CREATE TABLE TABLE1 AS SELECT ID_NO, POS, DATE,
TIMESTAMP FROM TRANDATA WHERE DATE
BETWEEN '2014-11-01 00:00:00' AND NOW()
AND POS IN ('IW1','IW2','IW3','IW4','IW5');
SET EVENT TO KEEP BRINGING DATA OVER FROM TRANDATA EVERY MINUTE OF DAY
CREATE event TABLE1GENERATOR ON schedule every 1 minute do INSERT INTO
TABLE1(ID_NO,POS, DATE, TIMESTAMP) SELECT ID_NO,
POS, DATE FROM TRANDATA
WHERE DATE BETWEEN DATE_SUB(NOW(), INTERVAL 1 MINUTE) AND NOW() AND POS IN
('IW1','IW2','IW3','IW4','IW5');
CREATE SECOND TABLE AND FILTER ID_NO FOR FIRST DATE IT SEES (MIN)
CREATE TABLE TABLE1FILTERED AS SELECT TABLE1.*
FROM TABLE1
WHERE TABLE1.DATE BETWEEN '2014-11-01 00:00:00' AND NOW() and NOT EXISTS (
SELECT *
FROM TABLE1 T2_MIN
WHERE T2_MIN.ID_NO=TABLE1.ID_NO
AND T2_MIN.TIMESTAMP< TABLE1.TIMESTAMP
);
CREATE EVENT TO KEEP FILTERING DATA EVERY MINUTE -
CREATE event TABLE1FILTER ON schedule every 1 minute do INSERT INTO
TABLE1FILTERED (ID_NO, POS, DATE, TIME_STAMP) SELECT TABLE1.*
FROM TABLE1
WHERE TABLE1.DATE BETWEEN DATE_SUB(NOW(), INTERVAL 1 MINUTE) AND NOW()
and NOT EXISTS (
SELECT *
FROM TABLE1 T2_MIN
WHERE T2_MIN.ID_NO=TABLE1.ID_NO
AND T2_MIN.TIMESTAMP< TABLE1.TIMESTAMP
);
Related
I have a column in my sql table called loggedTime which is a datetime field and I want to select between two dates startDate and endDate along with the interval may be 5 minutes, 10 minutes, 1 hour etc. I tried to write the SQL query but it says You have syntax error next interval, I am not sure what wrong with my query. If I remove INTERVAL 5 MINUTE my query works fine but I want to pass the Interval along with the date so it will select all rows between two dates and also with interval
Here is SQL
SELECT * FROM mytable WHERE loggedTime BETWEEN '2021-06-01' and '2021-06-03' INTERVAL 5 MINUTE
If you have any unique consecutively increasing column like id, then you can use an INNER JOIN as done followingly:
SELECT *
FROM mytable a
INNER JOIN mytable b
ON a.ID = b.ID + 1
WHERE TIMESTAMPDIFF(minute, a.timestamp, b.timestamp) = 5;
If you do not have that column in your table then use this code :
SELECT *
FROM (SELECT mt.*,
TIMESTAMPDIFF(minute, #prevTS, `loggedTime`) AS timeinterval,
#prevTS:=mt.`loggedTime`
FROM mytable mt,
(SELECT #prevTS := (SELECT MIN(`loggedTime`)
FROM yourTable)) vars
ORDER BY ID)subquery_alias
WHERE loggedTime BETWEEN '2021-06-01' AND '2021-06-03'
AND timeinterval = 5
Check this thread as reference too.
So I have 'table1' I need to insert data from 'table2' where 'table2'.'created_at' is from the past 24hrs.
Don't have much experience in SQL so any advice its welcome.
I have this query for now, but the clients using the website are in different timezone.
INSERT INTO 'table1' (
col1, col2, col3, col4
)
SELECT
NOW(), 0, table2.col3, COUNT(*)
FROM
'table2'
WHERE
DATEDIFF(
table2.created_at, CURDATE()
) >= -1
GROUP BY
table2.col3
Can I make the query to get the created_at as a datetime, get the current datetime, and count the data created in the past 24 hours rather than basing the query off of dates?
UPDATE
what I was needed in the where clause is:
WHERE created_at >= NOW() - INTERVAL 24 HOUR
I'm having some difficulties on applying certain conditions if a column is empty or not.
My table is as follows:
CREATE TABLE `meets` (
`id` INT,
`scheduled` VARCHAR(255),
`status` INT
);
INSERT INTO meets(id,scheduled,status) VALUES (1,'','1');
INSERT INTO meets(id,scheduled,status) VALUES (2,'','2');
INSERT INTO meets(id,scheduled,status) VALUES (2,'1613220631','3'); // in 30 minutes
INSERT INTO meets(id,scheduled,status) VALUES (2,'1644756631','3'); // 2022
What I did so far is next:
SELECT * FROM meets WHERE FROM_UNIXTIME(scheduled) BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL -30 MINUTE) ORDER BY `id` DESC
The above only selects the record that has a timestamp in the following/past 30 minutes.
Other than that record, I also need to select record id 1 because it has status == 1.
So basically
if scheduled column is empty, check for status to be 1 and select if true;
if scheduled column is timestamp, apply condition from the query posted above;
Any ideas? Thank you!
You could add the missing rows using union
SELECT *
FROM meets
WHERE FROM_UNIXTIME(scheduled)
BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL -30 MINUTE)
ORDER BY `id` DESC
UNION
select * from meets where scheduled = '' AND status = 1
What is an efficient way to get all records with a datetime column whose value falls somewhere between yesterday at 00:00:00 and yesterday at 23:59:59?
SQL:
CREATE TABLE `mytable` (
`id` BIGINT,
`created_at` DATETIME
);
INSERT INTO `mytable` (`id`, `created_at`) VALUES
(1, '2016-01-18 14:28:59'),
(2, '2016-01-19 20:03:00'),
(3, '2016-01-19 11:12:05'),
(4, '2016-01-20 03:04:01');
If I run this query at any time on 2016-01-20, then all I'd want to return is rows 2 and 3.
Since you're only looking for the date portion, you can compare those easily using MySQL's DATE() function.
SELECT * FROM table WHERE DATE(created_at) = DATE(NOW() - INTERVAL 1 DAY);
Note that if you have a very large number of records this can be inefficient; indexing advantages are lost with the derived value of DATE(). In that case, you can use this query:
SELECT * FROM table
WHERE created_at BETWEEN CURDATE() - INTERVAL 1 DAY
AND CURDATE() - INTERVAL 1 SECOND;
This works because date values such as the one returned by CURDATE() are assumed to have a timestamp of 00:00:00. The index can still be used because the date column's value is not being transformed at all.
You can still use the index if you say
SELECT * FROM TABLE
WHERE CREATED_AT >= CURDATE() - INTERVAL 1 DAY
AND CREATED_AT < CURDATE();
You can use subdate to indicate "yesterday" and use date() to indicate that you want records where just the date part of the column matches. So:
SELECT *
FROM tablename
WHERE DATE(created_at) = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
Here is the same question with an answer. To summarize answer for you, use subdate() as suggested by Sajmon.
subdate(currentDate, 1)
using your table it should be.
select *
from tablename
where created_at between subdate(CURDATE(), 1)
and date (now() )
use:
subdate(current_date, 1)
it's awesome for your case!
SELECT subdate(current_date(), 1)
SELECT * FROM table
WHERE created_at >= subdate(current_date(), 1)
You can use this, just put tablename and columnName (Which Contain 2021/01/09 or 2022-01-11 14:56:07 etc)
select * from (TABLENAME) where DATE(columnNAME) = TODAY - 1;
I use:
SELECT * FROM data WHERE date >= now() - INTERVAL 1 DAY
To fetch all records in the last 24h. Is there a way to "filter" from MySQL to fetch only one value of each hour in that 24h period? I dont really mind if its the 1st record or the last record of every hour.
date is in Mysql timestamp and is used as the primary key (CURRENT_TIMESTAMP).
Why? i insert a temperature record every minute and then i draw it using Google Chart. When i draw the chart of the past 24h it looks crazy!! Since its a home DIY project im fine with drawing the temperature chart in the last 24h with 1h interval.
Try this:
SELECT * FROM data WHERE date >= now() - INTERVAL 1 DAY group by hour(date)
You can group the data by the hour and select for instance the lowest id for every hour. If you join that subquery against your table - you will get the desired result
select d1.*
from data d1
join
(
SELECT min(id) as id
FROM data
group by date(date), hour(date)
) d2 on d1.id = d2.id
WHERE d1.date >= now() - INTERVAL 1 DAY