I'm very new to mysql so excuse me, I need to get records of the last month of the soldAt column, so for e.g. there's a soldAt date yyyy-mm-dd 2022-05-12 I get the record for its whole previous month. I'm using this query
SELECT at.*
from Analytics as at
where MONTH(at.SoldAtUpdatedAt) = MONTH(NOW() - INTERVAL 1 MONTH)
and YEAR(at.SoldAtUpdatedAt)= YEAR(NOW())
This query works fine and i get previous month results for current year but what if my soldAtUpdatedAt has a date of 2022-01-01 i.e. 1st of january of 2022 i want to get the december month record for year 2021. How do i query this part? I read about Datediff but i get 0 rows when i fetch.. Any guidance would be appreciated!. Also I tried (datepart didnt work in mysql) dateadd , datediff but i get zero rows...
Optimal query is
SELECT *
FROM Analytics
WHERE SoldAtUpdatedAt >= DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y-%m-01')
AND SoldAtUpdatedAt < DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
DATE_FORMAT(date_value_or_expression, '%Y-%m-01') creates the date for 1st day of date's month (with zero timepart). This expression is constant, hence it is calculated once - and not while execution but as a step of the execution plan building.
This query is SARGable - the query may use the index by SoldAtUpdatedAt.
This WHERE expression gets you the current month.
WHERE SoldAtUpdatedAt >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND SoldAtUpdatedAt < LAST_DAY(NOW()) + INTERVAL 1 DAY
This one gets you the same month a year ago.
WHERE SoldAtUpdatedAt >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
AND SoldAtUpdatedAt < LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 12 MONTH
So, something like this should get you a summary of your table for those two months.
SELECT LAST_DAY(SoldAtUpdatedAt) month_ending, SUM(sales) sales, COUNT(*) numb
FROM Analytics
WHERE ( SoldAtUpdatedAt >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND SoldAtUpdatedAt < LAST_DAY(NOW()) + INTERVAL 1 DAY)
OR ( SoldAtUpdatedAt >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
AND SoldAtUpdatedAt < LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 12 MONTH)
GROUP BY LAST_DAY(SoldAtUpdatedAt)
ORDER BY LAST_DAY(SoldAtUpdatedAt);
LAST_DAY() comes in handy here both for month computations and grouping. It happens to be portable outside MySQL.
DATE_FORMAT(NOW(), '%Y-%m-01') also works to get the first day of the present month.
Related
How to select all data from last month (or 30 days)?
I already found some answers, and mostly gives this solution
SELECT *
FROM gigs
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
ORDER BY date DESC
But this gives me also the dates from the future
I am only interested in the days from last month or 30 days (not next month and beyond)
Is this what you want?
WHERE date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND date <= CURRENT_DATE
I added a condition so the query filters on date not greater than today. I also modified your code so the date range starts one month ago (you had 3 months).
try this code
SELECT * FROM gigs
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
ORDER BY date DESC
You are asking for two separate things.
The last 30 days is easy.
date between now() - interval 30 day and now()
Data this month is like this:
date between (last_day(Now() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) and last_day(Now())
Data a few months ago is like this:
date between (last_day(Now() - INTERVAL 4 MONTH) + INTERVAL 1 DAY)
and
(last_day(Now() - INTERVAL 3 MONTH) + INTERVAL 1 DAY)
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
I'm doing a review of existing code and have found the following SQL query which is used to get a selection of records last month.
Is there a more concise way of writing SQL to do what this date based clause does in MySQL?
SELECT foo
FROM some_table
WHERE some_date
BETWEEN
DATE_FORMAT(LAST_DAY((NOW() - INTERVAL 1 MONTH) - INTERVAL 1 SECOND), '%Y-%m-01 00:00:00')
AND
DATE_FORMAT(LAST_DAY((NOW() - INTERVAL 1 MONTH) - INTERVAL 1 SECOND), '%Y-%m-%d 23:59:59')
It works, but I just twitch a little every time I see it.
Can anyone else write it better?
Thank you in advance.
There's no need to format the dates, they default to YYYY-MM-DD 00:00:00.
This is a little bit simpler:
SELECT foo
FROM some_table
WHERE some_date >= LAST_DAY(CURDATE() - INTERVAL 2 MONTH) + INTERVAL 1 DAY
AND some_date < LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY
So if CURDATE() is today, 2019-02-06, then:
- INTERVAL 2 MONTH is 2018-12-06
LAST_DAY() of that date is 2018-12-31
+ INTERVAL 1 DAY is 2019-01-01
Then the upper bound is:
- INTERVAL 1 MONTH is 2019-1-06
LAST_DAY() of that date is 2019-1-31
+ INTERVAL 1 DAY is 2019-02-01
The dates should be strictly less than 2019-02-01.
Using less than accounts for timestamps in the last second of the month, between 23:59:59.000 and 23:59:59.999.
How do I SELECT the first week of a previous month I've tried
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>=(CURDATE()- 1 WEEK - INTERVAL 2 week)";
This was used to try and select the third week but this didn't work
Does this work for you:
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>= curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week
AND startDate < curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week _+ interval 1 week";
?
The idea here is that we first go back a month, then find the start of the week (assuming Monday, for Sunday we will need some extra tweaking), then figure out how many whole weeks it has been from the start of the month, and subtract that many weeks from the date so far. This takes us back to the start of the first week of the month. For the end of the range we just add one week to the start.
Ah, your question became more detailed.
Not really familiar with sql, there might be better but something like:
SELECT repairId , startDate,catId, statusId FROM repair
WHERE EXTRACT(YEAR_MONTH FROM start_date) = EXTRACT(YEAR_MONTH FROM NOW()) - 1 AND CAST(EXTRACT(DAY FROM start_date) / 7 + 1 as INT) = ?;
Basically, extract the year month components to compare year and month and then extract the day of month use the flooring caused by integer truncation to get the week to compare with whatever week you are looking for
mysql return rows matching year month
I have a table with a timestamp field. How do I get data from the last 3 months?
In particular, March is my current month let say, 03/2012. I need to return records from the months March, February, and January only.
3 months before today:
select * from table where timestamp >= now()-interval 3 month;
Start with first of month:
select * from table where timestamp >= last_day(now()) + interval 1 day - interval 3 month;
To get the first day of the current month, you could use this:
DATE_FORMAT(CURDATE(), '%Y-%m-01')
if current date is 2013-03-13, it will return 2013-03-01, and we can just substract 2 months from this date to obtain 2013-01-01. Your query could be like this:
SELECT *
FROM yourtable
WHERE data >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH
I know this is an old question, but to possibly save others time and to sum the above answers up for the case of needing (1) dates from current month and (2) dates from the prior 2 months (common when displaying data statistics):
WHERE ((timestamp >= NOW() - DATE_FORMAT(CURDATE(), '%Y-%m-01'))
OR (timestamp >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH))
Assuming you're using SQL Server (Oracle, MySQL and others have similar date functions), you can use the dateadd function to add or subtract an interval to the current date.
If you want a full three months, you can subtract 3 months from today : DATEADD(m,-3,getdate())
But, as you state, you only want data from January, February and March. You have to make some calculation based on today's date: dateadd(m,-2, CONVERT(datetime, CONVERT(VARCHAR(2), MONTH(getdate())) + '/01/' + CONVERT(VARCHAR(4), YEAR(getdate()))))
And in the end, get a query like
SELECT fields
FROM table
WHERE timestampfield > DATEADD(m,-2, CONVERT(datetime, CONVERT(VARCHAR(2), MONTH(getdate())) + '/01/' + CONVERT(VARCHAR(4), YEAR(getdate()))))
--- edit ---
erf, I just noticed the "mysql" tag... you can get more information on MySQL date functions here : https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Another possibility would be:
SELECT * WHERE your_date_column > LAST_DAY(CURRENT_DATE - INTERVAL 3 MONTH);
Use this code here to get the previous 3 months from a certain date
SELECT * FROM table WHERE date_column>= DATE_FORMAT(current_date(), '%Y-%m-01') - INTERVAL 3 MONTH and date_column< DATE_FORMAT(current_date(), '%Y-%m-01')
WHERE ((timestamp >= NOW() - DATE_FORMAT(CURDATE(), '%Y-%m-01'))
OR (timestamp >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH))