MySQL query last 30 days sort by day - mysql

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;

Related

MySQL - How to find records for yesterday when timestamp is part of date

My MySQL table stores records with a date/time stamp. I am wanting to find records from the table that were created yesterday (as in have a creation date of yesterday - regardless of what the timestamp portion is)
Below is what a db record looks like:
I have tried the following select (and a few other variations, but am not getting the rows with yesterday's date.
SELECT m.meeting_id, m.member_id, m.org_id, m.title
FROM meeting m
WHERE m.create_dtm = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
Not exactly sure how I need to structure the where clause to get meeting ids that occurred yesterday. Any help would be greatly appreciated.
A naive approach would truncate the creation timestamp to date, then compare:
where date(m.create_dtm) = current_date - interval 1 day
But it is far more efficient to use half-open interval directly against the timestamp:
where m.create_dtm >= current_date - interval 1 day and m.create_dtm < current_date
You can try next queries:
SELECT
m.meeting_id, m.member_id, m.org_id, m.title, m.create_dtm
FROM meeting m
-- here we convert datetime to date for each row
-- it can be expensive for big table
WHERE date(create_dtm) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
SELECT
m.meeting_id, m.member_id, m.org_id, m.title, m.create_dtm
FROM meeting m
-- here we calculate border values once and use them
WHERE create_dtm BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND DATE_SUB(CURDATE(), INTERVAL 1 SECOND);
Here live fiddle: SQLize.online

Add Week Interval to Date Query

I have the following SQL Query which delivers data well for current date, how can i set this query to query a week of data from curdate?
SELECT count( * ) as today_total_4
FROM cdr
WHERE dcontext='ext-queues' AND DATE(`calldate`) = DATE(CURDATE())
Many Thanks.
You can use a DATE_ADD function to get the date a week from now. (Either forward or back)
http://www.w3schools.com/sql/func_date_add.asp
SELECT count( * ) as today_total_4
FROM cdr
WHERE dcontext='ext-queues' AND DATE(`calldate`) = DATE_ADD(CURDATE(), INTERVAL 7 DAY)
Depending on your query requirements you can calculate 7 days back using:
DATE_ADD(CURDATE(), INTERVAL -7 DAY)
If you wanted your query to be everything since a week ago, then you'd change your query to have the following where clause:
WHERE dcontext='ext-queues' AND DATE(calldate) > DATE_ADD(CURDATE(), INTERVAL -7 DAY)
SELECT count( * ) as today_total_4
FROM cdr
WHERE dcontext='ext-queues' AND WHERE WEEK(`calldate`) = WEEK(DATE(CURDATE()))

Return row count between 2 dates always returns 0

wonder if any aficionados can help me. I am trying to get the number of records between two dates, I made this query really simple using some posts i found on here, it always returns 0.
date_added is a timestamp
SELECT COUNT(id)
FROM item
WHERE date_added >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND date_added <= NOW()
I also tried this:
SELECT COUNT(id)
FROM item
WHERE date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW()
What am I doing wrong?
Use DATE(NOW()) and DATE(date_added) for comparison of dates in mysql. It will give you date part of the timestamp.
For Example
CURDATE() is 2008-11-11 and
NOW() is 2008-11-11 12:45:34
One is date, other is timestamp. You should take either both dates or both timestamp. One solution I've told above.
I re ran this query this morning and it worked just fine, so I'm not sure what went wrong last night other than the hour. Thanks to everyone's input.
SELECT COUNT(id)
FROM item
WHERE date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()
worked fine as did
SELECT COUNT(id)
FROM item
WHERE DATE(date_added) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()
Hope that may be of use to someone.

How to do : "Between TODAY AND TODAY-7"?

I need to find the account created for the current day, et for the last 7 days.
To find my results for today, it works, and I do this :
SELECT * FROM `account` where DATE(created_at) = DATE(NOW())
But I don't know how to do to get the last 7days account.
I tried something like this, but without success :
SELECT * FROM `account` where DATE(created_at) BETWEEN DATE(NOW()) AND DATE(NOW()-7)
Have you an idea ?
in mysql:
SELECT * FROM `account`
WHERE DATE(created_at) > (NOW() - INTERVAL 7 DAY)
Try:
BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
If created_at has an index and you wouldn't like to prevent the optimiser from using it, I would recommend the following pattern (assuming created_at contains both date and time):
WHERE created_at >= CURRENT_DATE - INTERVAL 7 DAY
AND created_at < CURRENT_DATE + INTERVAL 1 DAY
This spans the range from the day exactly one week ago till today (inclusive), so 8 days in total.
also have a look at MySQL functions ADDDATE(), DATE_ADD(), DATE_SUB()
e.g.
ADDDATE(DATE(NOW()), -7)

Fetching rows added last hour

I keep a record of logins in a table. I have columns for id, ip, date and time. From that record of logins I wanna fetch logins made only in the last hour.
I'm sweeping through the MySQL docs on time and date functions, but I just can't seem to combine them correctly.
Can somebody help me?
Make use of the DATE_SUB() and NOW() functions:
select count(*) as cnt
from log
where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Hope it helps you : )
If you want to implement this into a cronjob, you need to specify the start and end.
For example, at 2pm, if you want to get the data for the past hour from 13:00:00 until 13:59:59, here's how to do it:
dateField BETWEEN
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 HOUR), '%Y-%m-%d %H:00:00')
AND
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 HOUR), '%Y-%m-%d %H:59:59')
it can be done easily using
select count(*) from logins where datetime>= NOW()- INTERVAL 1 HOUR
I recommend have one datetime column instead of date and time columns.
Suppose you have a datetime column called last_login:
SELECT id, ip_address, last_login
FROM mytable
WHERE last_login >= DATE_SUB(NOW(), interval 1 hour);
without the specifics, I think Date_Add() would work.. by adding to your where clause an add of NOW negative hours
(or Date_Sub() )
You can also use CURDATE()function
SELECT
count(*) AS TotalCount
FROM
tblName
WHERE
datetime >= DATE_SUB(CURDATE(), INTERVAL 1 HOUR)