Get data from last 48 hours - MySQL - mysql

I have an sql query and I need to get the count from the last 48 hours.
sql query
SELECT count(*), created
AS countMeta
FROM mimesi_indexer.meta_served_clips
GROUP BY created > DATE(NOW() - INTERVAL 48 HOUR)
I tried this query but it's not working, can someone tell me how to fix it or how to do it from scratch please?
Thanks

If created is a timestamp field and you want to count all rows created in the last 48 hours, your query should be written as this:
select
count(*) AS countMeta
from
mimesi_indexer.meta_served_clips
where
created > NOW() - INTERVAL 48 HOUR
If you want to show the count of every hour in the last 48 hours, one way is this:
select
date_format(created, '%Y/%m/%d %h') as day_hour_created,
count(*)
from
mimesi_indexer.meta_served_clips
where
created > NOW() - INTERVAL 48 HOUR
group by
date_format(created, '%Y/%m/%d %h')
using the string function date_format we can extract only the day and hour information from the created field, and group by this string. Then you might also want to change the condition
created > NOW() - INTERVAL 48 HOUR
to this:
created > date_format(now()- interval 48 hour, '%Y/%m/%d %h')
to skip the minute and second part, in order to make the first hour complete.

you should use mysql DATE_SUB() instead of DATE() function to get the records of last 48 hours
SELECT count(*), created
AS countMeta
FROM mimesi_indexer.meta_served_clips
GROUP BY created > DATE_SUB(now(), INTERVAL 48 HOUR)

Related

Selecting records from last 24 hours - MySQL

I'm trying to run this MySQL command to select records with a lead_submitted date in the last 24 hours.
The issue is it grabs future dates too (fyi I'm delaying some leads so basically set a date in the future for these to be processed).
For example, if I run the command:
SELECT id, lead_submitted, processed FROM leads WHERE (lead_submitted > now() - INTERVAL 24 HOUR) ORDER BY id DESC;
I get this result:
10 | 2022-10-04 13:24:13 | N
~
Why is this? I just want to select records from the last 24h.
FTR, when I run SELECT NOW() as now; I get 2022-09-30 14:00:12
Any help would be greatly appreciated.
EDIT:
I've found this works as a workaround, but it feels nasty:
SELECT id, lead_submitted, processed FROM leads WHERE (lead_submitted > now() - INTERVAL 24 HOUR) AND (lead_submitted < now() + INTERVAL 1 SECOND) AND processed = 'N' ORDER BY id DESC LIMIT 1;
You probably want to use:
SELECT id, lead_submitted, processed
FROM leads
WHERE lead_submitted between (now() - INTERVAL 24 HOUR) and now()
ORDER BY id DESC;

Find a record between two dates MySQL

I am attempting to find records in my DB that's end_date is within 24 hours of the current time.
Here is the query I am currently running
select *
FROM record r
WHERE NOW() BETWEEN r.end_date AND DATE_ADD(r.end_date, INTERVAL -1 DAY);
This doesn't bring back any of the records with an end_date within 24 hours, am I using the DATE_ADD function wrong?
I think this is what you need:
select *
FROM record r
where
r.end_date >= date_add(now(), INTERVAL -1 DAY)

Get hourly result for count in the last 48 hours - MySql

I need a query that returns the count every hour for the last 48 hours.
Until now I tried either:
SELECT count(*), date_format(created, '%H:%i - %d/%m/%y') as datecreated
FROM mimesi_indexer.served_clips
where created > NOW() - INTERVAL 24 HOUR
group by floor(hour(created))
order by created ASC
and:
SELECT count(*), date_format(created, '%H:%i - %d/%m/%y') as datecreated
FROM mimesi_indexer.served_clips
where created > NOW() - INTERVAL 48 HOUR
group by floor(hour(created))
order by created ASC
But neither of those give the wanted result.
Do you have any idea on how I could do this?
Thanks
If you want every hour, don't you want this:
SELECT count(*), date_format(created, '%H - %d/%m/%y') as datecreated
FROM mimesi_indexer.served_clips
where created > NOW() - INTERVAL 24 HOUR
group by date_format(created, '%H - %d/%m/%y')
order by min(created) ASC;
That is, you need to include the day as well as the hour -- and remove the minutes from datecreated.

MySQL query last 30 days sort by day

I have a query that I need to modify to use data variables to search counts for the last 30 days from today and group by date. My SQL server's /tmp/ directory (15GB) keeps filling up and the query fails.
SELECT DATE(`date_time`) AS DAY
, COUNT(DISTINCT(rcid)) AS COUNT
, COUNT(DISTINCT(tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE"%VOE%"
AND DATE_SUB(NOW(), INTERVAL 30 DAY) and NOW()
GROUP BY DAY;
Can you run the query manually in a shell? Looks like you're almost there. It may be failing on the group by. At this point in the execution it doesn't know what column DAY is.
Try: GROUP BY DATE(date_time)
Instead of: GROUP BY DAY.
Your date check is missing the column name to test, and also missing the BETWEEN keyword between the two dates.
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%"
AND date_time BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY DAY;
Try this:
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE
"%VOE%" AND `date_time` BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY `date_time`;
Thanks all. I got t to work using CURDATE()
AND `date_time` BETWEEN CURDATE() - INTERVAL 30
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%" AND `date_time` BETWEEN CURDATE() - INTERVAL 30 DAY AND NOW() GROUP BY DAY;

How to dynamically pull MYSQL date queries?

I am trying to select data from two periods (Last 30 days and Previous 30 days)
So two 30 day periods.
I have the last 30 day period down:
SELECT ProductID, ProductIDintarget,date_format(Date,'%m/%d/%Y'),SUM(Rev)
FROM datatable
WHERE Date BETWEEN CURDATE()-1 - INTERVAL 31 DAY AND CURDATE()
GROUP BY ProductIDintarget
That is working fine, but now I'm struggling to get the previous 30 day period.
I have tried changing the WHERE statement as:
WHERE Date BETWEEN CURDATE()-32 - INTERVAL 31 DAY AND CURDATE()-32
but no luck..
You can use DATE_ADD() or DATE_SUB() like this:
SELECT
ProductID,
ProductIDintarget,
DATE_FORMAT(Date,'%m/%d/%Y'),
SUM(Rev)
FROM datatable
WHERE `Date` < (DATE_ADD(CURDATE(), INTERVAL 1 MONTH))
AND `Date` > (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
GROUP BY
ProductID,
ProductIDintarget,
`Date`
Note:- You should backticks if you have keywords as column names.