Average time between two datetimes for this month and last month - mysql

We have a table with two fields tkTimeOpen and tkTimeClosed. We need to find the average wait time for this month and last month. I've been unable to get the right SQL query to pull out what I need.
This is how the date-time is recorded; 2017-01-25 10:35
This Month's Average;
SELECT SUM(DATEDIFF(MINUTE,tkTimeOpen,tkTimeClose)) * 1.0
/ (SELECT COUNT(*) * 1.0 FROM e_ticket)
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(tkTimeOpen) = MONTH(CURRENT_DATE - INTERVAL 0 MONTH)
Last Month's Average
SELECT SUM(DATEDIFF(MINUTE,tkTimeOpen,tkTimeClose)) * 1.0
/ (SELECT COUNT(*) * 1.0 FROM e_ticket)
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(tkTimeOpen) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
I've tried a lot of variations but it doesn't give the desired output.
If anyone could help that would be great!
Thank you

This should give you the average minutes for last month and this month combined:
SELECT AVG(TIMESTAMPDIFF(MINUTE, tkTimeOpen, tkTimeClose))
FROM e_ticket
WHERE tkTimeOpen >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY);
If you still need to get the averages separately, you can keep your WHERE clause similar to what you have...
For this month:
SELECT AVG(TIMESTAMPDIFF(MINUTE, tkTimeOpen, tkTimeClose))
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(NOW()) AND MONTH(tkTimeOpen) = MONTH(NOW());
And for last month:
SELECT AVG(TIMESTAMPDIFF(MINUTE, tkTimeOpen, tkTimeClose))
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(NOW() - INTERVAL 1 MONTH) AND MONTH(tkTimeOpen) = MONTH(NOW() - INTERVAL 1 MONTH);

One function you could use is TIMESTAMPDIFF - not DATEDIFF
You can use AVG() instead of SUM() / COUNT()
SELECT AVG(TIMESTAMPDIFF(MINUTE,tkTimeOpen,tkTimeClose))
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(tkTimeOpen) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)

Related

SQL comparing date

I want to compare the current date with a date from a database table.
This code:
DATE_FORMAT(dt,'%y-%m')
gives me a date like this 2020-10
and i want that this code:
DATE_ADD(CURRENT_DATE, INTERVAL - 1 month)
gets date like 2020-10 not 2020-10-12(some day)
Here is the whole sql query:
select count(*)
from app_tickets t
where t.status= 3
and DATE_FORMAT(dt,'%y-%m')=DATE_ADD(CURRENT_DATE, INTERVAL - 1 month)
Thanks for help:)
This can make use of indexes
select count(*)
from app_tickets t
where t.status = 3
and t.dt >= DATE_FORMAT(CURRENT_DATE - interval 1 month ,'%Y-%m-01')
and t.dt < DATE_FORMAT(CURRENT_DATE ,'%Y-%m-01')

Get records from last one year upto last date of previous month in mysql

I am trying to get records from last one year and upto last date of provious month i.e. not including the current month. Here's my query:
SELECT `customer_id`, `customer_name`, `customer_date`
FROM `customers`
WHERE DATE(`customer_date`) <= (CURDATE() - INTERVAL 1 MONTH)
AND DATE(customer_date) >= (CURDATE() - INTERVAL 12 MONTH)
This query fetches records from 1 May, 2018 to 8 April 2019.
INTERVAL 1 MONTH fetches records 30 days ago. What I need to do something here?
I want to exclude current month records, so query should return records upto 30 April 2019. How do we do that?
You must correctly calculate the first and last days of range with help LAST_DAY() function. For example:
Calculate the first day of the range
SELECT LAST_DAY(CURDATE() - INTERVAL 13 MONTH) + INTERVAL 1 DAY
Output:
2018-05-01
Calculate last day of the range
SELECT LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
Output:
2019-04-30
The full query might look like:
SELECT `customer_id`, `customer_name`, `customer_date`
FROM `customers`
WHERE `customer_date` >= LAST_DAY(CURDATE() - INTERVAL 13 MONTH) + INTERVAL 1 DAY
AND `customer_date` <= SELECT LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
To get data up to the previous month:
where customer_date < curdate() + interval (1 - day(curdate()) day
Why? First note that there is no function call on the customer_date. So, this expression is index-compatible and can use an index.
Second, this structure works both for dates and date/times. That is very handy, because it may not always be obvious if a column has a time component (people are not very good about naming columns to capture this information).
You claim that the "12 months" ago portion works. That doesn't look correct to me. For the complete logic:
where customer_date < curdate() + interval (1 - day(curdate()) day and
customer_date >= (curdate() + interval (1 - day(curdate()) day) - interval 1 year)
SELECT `customer_id`, `customer_name`, `customer_date` FROM `customers` WHERE
MONTH(`customer_date`) <= (CURDATE() - INTERVAL 1 MONTH) AND
MONTH(customer_date) >= (CURDATE() - INTERVAL 12 MONTH)
hope this help

Mysql retrieve orders from last year in the same month as today

I have a table orders and I would like to make a request to retrieve all orders from the month of last year to the same month as now. Example: we are in November, I would like to recover all the orders of the month of November 2017.
I try something like that but it does not work:
SELECT COUNT(*)
FROM ps_orders o
WHERE YEAR(o.date_add) = YEAR(NOW() - INTERVAL 1 YEAR)
AND MONTH(o.date_add) = MONTH(NOW() - INTERVAL 1 MONTH)
Thank you for your help
You dont need to subtract the month. It should be MONTH(NOW()) to get the same month results from the previous year.
SELECT COUNT(*)
FROM ps_orders o
WHERE YEAR(o.date_add) = YEAR(NOW() - INTERVAL 1 YEAR)
AND MONTH(o.date_add) = MONTH(NOW()) -- same as current month
I would recommend doing this with date ranges:
SELECT COUNT(*)
FROM ps_orders o
WHERE o.date_add >= (curdate() - interval (day - 1) day) - interval 12 month AND
o.date_add >= (curdate() - interval (day - 1) day) - interval 11 month ;
When you use functions such as YEAR() and MONTH() the optimizer will not take full advantage of an available index on date_add. However, this version can use an appropriate index.

How do I retrieve data for the previous month in SQL

I want to get data for the dates between 2015-05-01 and 2015-06-01 using SQL.
Please help me with the query.
The query I used is:
select *,count(id) as multiple_visitors
from table1
where id=123
and (date(server_time) between (CURDATE() - INTERVAL 31 DAY) AND CURDATE())
group by user_id having count(id)>1
You can do this with month() and year():
where month(server_time) = month(curdate() - interval 1 month) and
year(server_time) = year(curdate() - interval 1 month)
However, I recommend a slightly more complex expression:
where server_time >= date_sub(date_sub(curdate(), interval - day(curdate()) + 1 day), interval 1 month) and
server_time < date_sub(curdate(), interval - day(curdate()) + 1 day)
The advantage is that there are no functions on server_time, so the database engine can use an index, if appropriate.
As a note: the expression date_sub(curdate(), interval - day(curdate()) + 1 day) gets midnight on the first day of the month.
Try using "WHERE" with MONTH(date).
Like this:
SELECT * FROM Table
WHERE MONTH(date) = 1

Query to get all rows from previous month

I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Here's another alternative. Assuming you have an indexed DATE or DATETIME type field, this should use the index as the formatted dates will be type converted before the index is used. You should then see a range query rather than an index query when viewed with EXPLAIN.
SELECT
*
FROM
table
WHERE
date_created >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )
AND
date_created < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
If there are no future dates ...
SELECT *
FROM table_name
WHERE date_created > (NOW() - INTERVAL 1 MONTH);
Tested.
Alternatively to hobodave's answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
You could achieve the same with EXTRACT, using YEAR_MONTH as unit, thus you wouldn't need the AND, like so:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
SELECT *
FROM yourtable
where DATE_FORMAT(date_created, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 month),'%Y-%m')
This should return all the records from the previous calendar month, as opposed to the records for the last 30 or 31 days.
Even though the answer for this question has been selected already, however, I believe the simplest query will be
SELECT *
FROM table
WHERE
date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();
WHERE created_date >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND created_date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY)
This worked for me (Selects all records created from last month, regardless of the day you run the query this month)
Alternative with single condition
SELECT * FROM table
WHERE YEAR(date_created) * 12 + MONTH(date_created)
= YEAR(CURRENT_DATE) * 12 + MONTH(CURRENT_DATE) - 1
select fields FROM table
WHERE date_created LIKE concat(LEFT(DATE_SUB(NOW(), interval 1 month),7),'%');
this one will be able to take advantage of an index if your date_created is indexed, because it doesn't apply any transformation function to the field value.
Here is the query to get the records of the last month:
SELECT *
FROM `tablename`
WHERE `datefiled`
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH )
AND
LAST_DAY( DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH ) )
Regards
- saqib
if you want to get orders from last month, you can try using
WHERE MONTH(order_date) = MONTH(CURRENT_DATE()) -1
One more way to do this in:
MYSQL
select * from <table_name> where date_created >= DATE_ADD(NOW(), INTERVAL -30 DAY);
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)