I have a dateBill field -DATETIME- in my DB.
I would like to retrieve all the info from THE LAST MONTH.
So, today is: 2011-08-02 12:00:00
My query is:
SELECT *
FROM bills
WHERE DATE(dateBill) > DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND status = 1
ORDER BY id_bill
The status just says if the bill is approved or not.
But I get some weid results:
2011-09-01 21:44:07
2012-08-01 00:00:00
I inserted those values just to test. As you can see, it is not working.
Any help, please?
DATE(dateBill) > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
captures dates in the future too.
including next month
2011-09-01 21:44:07
and next year
2012-08-01 00:00:00
add AND CURDATE() <= DATE(dateBill) if you dont like that...
SELECT *
FROM bills
WHERE
YEAR(dateBill) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) AND
MONTH(dateBill) = MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) AND
status = 1
ORDER BY id_bill
Related
I am trying to write a query to get the last 4 weeks (Mon-Sun) of data. I want every week of data to be stored with an individual and shared table.
every week data store based on name if same name repeated on single week amt should sum and if multiple name it should be show data individual, To see an example of what I am looking for, I have included the desired input and output below.
this is my table
date
amt
name
2022-04-29
5
a
2022-04-28
10
b
2022-04-25
11
a
2022-04-23
15
b
2022-04-21
20
b
2022-04-16
20
a
2022-04-11
10
a
2022-04-10
5
b
2022-04-05
5
b
i want output like this
date
sum(amt)
name
2022-04-25 to 2020-04-29
16
a
2022-04-25 to 2020-04-29
10
b
2022-04-18 to 2022-04-24
35
b
2022-04-11 to 2022-04-17
30
a
2022-04-04 to 2022-04-10
10
b
I would appreciate any pointers or 'best-practises' which I should employ to achieve this task.
You can try to use DATE_ADD with WEEKDAY get week first day and end day.
SELECT
CASE WHEN
weekofyear(`date`) = weekofyear(NOW())
THEN 'current week'
ELSE
CONCAT(date_format(DATE_ADD(`date`, interval - WEEKDAY(`date`) day), '%Y-%m-%d'),' to ',date_format(DATE_ADD(DATE_ADD(`date`, interval -WEEKDAY(`date`) day), interval 6 day), '%Y-%m-%d'))
END 'date',
SUM(amt)
FROM T
GROUP BY
CASE WHEN
weekofyear(`date`) = weekofyear(NOW())
THEN 'current week'
ELSE
CONCAT(date_format(DATE_ADD(`date`, interval - WEEKDAY(`date`) day), '%Y-%m-%d'),' to ',date_format(DATE_ADD(DATE_ADD(`date`, interval -WEEKDAY(`date`) day), interval 6 day), '%Y-%m-%d'))
END
sqlfiddle
EDIT
I saw you edit your question, you can just add name in group by
SELECT
CONCAT(date_format(DATE_ADD(`date`, interval - WEEKDAY(`date`) day), '%Y-%m-%d'),' to ',date_format(DATE_ADD(DATE_ADD(`date`, interval -WEEKDAY(`date`) day), interval 6 day), '%Y-%m-%d')) 'date',
SUM(amt),
name
FROM T
GROUP BY
CONCAT(date_format(DATE_ADD(`date`, interval - WEEKDAY(`date`) day), '%Y-%m-%d'),' to ',date_format(DATE_ADD(DATE_ADD(`date`, interval -WEEKDAY(`date`) day), interval 6 day), '%Y-%m-%d')),
name
ORDER BY 1 desc
sqlfiddle
This is in SQL Server, and just a mess about. Hopefully it can be of some help.
with cteWeekStarts
as
(
select
n,dateadd(week,-n,DATEADD(week, DATEDIFF(week, -1, getdate()), -1)) as START_DATE
from
(values (1),(2),(3),(4)) as t(n)
), cteStartDatesAndEndDates
as
(
select *,dateadd(day,-1,lead(c.start_date) over (order by c.n desc)) as END_DATE
from cteWeekStarts as c
)
,cteSalesSumByDate
as
(
select s.SalesDate,sum(s.salesvalue) as sum_amt from
tblSales as s
group by s.SalesDate
)
select c3.n as WeekNum,c3.START_DATE,isnull(c3.END_DATE,
dateadd(day,6,c3.start_date)) as END_DATE,
(select sum(c2.sum_amt) from cteSalesSumByDate as c2 where c2.SalesDate
between c3.START_DATE and c3.END_DATE) as AMT
from cteStartDatesAndEndDates as c3
order by c3.n desc
I need to do a select where I can chose to see results for current month, previous month, 1 month ago, 2 months ago, 3 months ago.
I found this question: MySQL: Query to get all rows from previous month, but I'm stuck with a filter that will get me all the results for 2 months ago from first to last day of the month.
I tried with this but it doesn't work:
SELECT * FROM table
AND MONTH(date_created) = MONTH(1 MONTH - INTERVAL 2 MONTH);
Try this:
SELECT * FROM table
WHERE MONTH(date_created) = MONTH(NOW() - INTERVAL 2 MONTH)
AND (
YEAR(date_created) = YEAR(NOW())
OR
YEAR(date_created) = YEAR(NOW() - INTERVAL 2 MONTH)
);
Returning records CREATED PRIOR the last 2 months only in MySQL.
If you want all rows from 2 months ago, then use logic like this:
WHERE date_created >= DATE_SUB(DATE_SUB(CURDATE(), 1 - DAY(CURDATE())), INTERVAL 2 MONTH) AND
date_created < DATE_SUB(DATE_SUB(CURDATE(), 1 - DAY(CURDATE())), INTERVAL 1 MONTH)
What is this doing? First, it is only applying functions to the current date part of the expression. This allows MySQL to use an index on date_created, if available and appropriate.
The expression DATE_SUB(CURDATE(), 1 - DAY(CURDATE()) is simply a way to get the first day of the month.
You query have an error, correct one would be:
SELECT * FROM table
WHERE MONTH(date_created) = MONTH(DATE_SUB(NOW(),INTERVAL 2 MONTH))
For current month just MONTH(NOW()), replace "2" with any number of months you need (1,3,.. 23)
as mentioned in comments this solution ignores YEAR differences, it just selects all records with the same month, no matter the year
you can filter wrong year results with additional clause:
AND YEAR(date_created) = '2019' # or year you need
Or use more complex query:
SELECT * FROM table
where
date_created between
/* first day of -2 month*/
date_sub(date_sub(now(),interval 2 month), interval (day(now())-1) day)
and
/* last day of -2 month*/
last_day(date_sub(now(),interval 2 month))
I'm trying to make a query that will select all my users who's donor status is ending within 10 days.
As i only want to send the message once I want to select all the users who has their donor status ending between 10 and 9 days ahead.
The Donor end date is in this format: 0000-00-00 00:00:00
This is the query I'm currently working around with:
SELECT UserID FROM users_info WHERE donorEnd BETWEEN (NOW() + INTERVAL 10 DAY) AND (NOW() + INTERVAL 9 DAY)
I think you problem is that you are adding a time not a date: NOW() + INTERVAL 9 DAY = 2015-02-27 19:19 not 2015-02-27 00:00
Try use ADDDATE with CURDATE():
SELECT UserID FROM users_info WHERE donorEnd BETWEEN DATE_ADD(CURDATE(), INTERVAL 9 DAY) AND DATE_ADD(CURDATE(), INTERVAL 9 DAY)
I would like to ask you that i want to find last working day in previous month in MYSQL.
How to do that ?
this code find last day previous month as the following:
LAST_DAY(Now()- INTERVAL 1 MONTH)
Result that i need as below:
Current Month= 2013-07-01====> Results should be 2013-06-28
Current Month= 2013-06-06====> Results should be 2013-05-31
How to find last working day in previous month ?
Regards
Here’s an example of what I thought of:
LAST_DAY(CURDATE() - INTERVAL 1 MONTH) -
INTERVAL (CASE WEEKDAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))
WHEN 5 THEN 1
WHEN 6 THEN 2
ELSE 0 END) DAY
Try something like:
SET #lastworkingday = DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE()) DAY);
// If saturday, take it back one day
IF DAYOFWEEK(#lastworkingday) = 7
THEN #lastworkingday := DATE_SUB(#lastworkingday, INTERVAL 1 DAY);
// If sunday, take it back two days
ELSE IF DAYOFWEEK(#lastworkingday) = 1
THEN #lastworkingday := DATE_SUB(#lastworkingday, INTERVAL 2 DAY);
There may be a more elegant way of writing this (I'd be surprised if there wasn't), but Gumbo's solution might look like this...
Note I've used #dt for testing. You could just substitute CURDATE() in your script.
SET #dt = '2012-07-01'; -- Last day of the previous month (June 2012) was a Saturday
SELECT CASE DAYOFWEEK(LAST_DAY(#dt-INTERVAL 1 MONTH))
WHEN 1 THEN LAST_DAY(#dt-INTERVAL 1 MONTH)-INTERVAL 2 DAY
WHEN 7 THEN LAST_DAY(#dt-INTERVAL 1 MONTH)-INTERVAL 1 DAY
ELSE LAST_DAY(#dt-INTERVAL 1 MONTH)
END x;
+------------+
| x |
+------------+
| 2012-06-29 | -- Friday 29th
+------------+
SET #dt = '2012-08-01'; -- Last day of the previous month (July 2012) was a Tuesday
SELECT CASE DAYOFWEEK(LAST_DAY(#dt-INTERVAL 1 MONTH))
WHEN 1 THEN LAST_DAY(#dt-INTERVAL 1 MONTH)-INTERVAL 2 DAY
WHEN 7 THEN LAST_DAY(#dt-INTERVAL 1 MONTH)-INTERVAL 1 DAY
ELSE LAST_DAY(#dt-INTERVAL 1 MONTH)
END x;
+------------+
| x |
+------------+
| 2012-07-31 | -- Tuesday 31st
+------------+
SET #dt = '2012-10-01'; -- Last day of the previous month (September 2012) was a Sunday
SELECT CASE DAYOFWEEK(LAST_DAY(#dt-INTERVAL 1 MONTH))
WHEN 1 THEN LAST_DAY(#dt-INTERVAL 1 MONTH)-INTERVAL 2 DAY
WHEN 7 THEN LAST_DAY(#dt-INTERVAL 1 MONTH)-INTERVAL 1 DAY
ELSE LAST_DAY(#dt-INTERVAL 1 MONTH)
END x;
+------------+
| x |
+------------+
| 2012-09-28 | -- Friday 28th
+------------+
Another way is to build a calendar table of all dates. Ignoring public holidays, the first and last working days of the month then are simply the min and max weekdays of the month.
Although, there is an argument that goes 'so why not just figure this bit out in your application code'!
To get last working date you have to follow following steps :
1. first of all get last date of last month .
2.check whether it is working day or not by weekday function . if it is sunday then subtract 2 from lastdate , if it is saturday then subtract 1 from lastdate otherwise do not change anything .
we can do this thing by case statement . check following query . it will really works :
SELECT
CASE WEEKDAY(LAST_DAY(CONCAT(YEAR(CURDATE()),'-' , MONTH(CURDATE())-1 ,'-' , 1)))
WHEN 6
THEN DATE_ADD(LAST_DAY(CONCAT(YEAR(CURDATE()),'-' , MONTH(CURDATE())-1 ,'-' , 1)),INTERVAL -2 DAY)
WHEN 5
THEN DATE_ADD(LAST_DAY(CONCAT(YEAR(CURDATE()),'-' , MONTH(CURDATE())-1 ,'-' , 1)),INTERVAL - 1 DAY)
ELSE
LAST_DAY(CONCAT(YEAR(CURDATE()),'-' , MONTH(CURDATE())-1 ,'-' , 1)) END;
http://sqlfiddle.com/#!2/d41d8/15940
if you subtract the day of current month then you will get the last day of previous month
(DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) DAY))
For example, on the 12th of the month if we subtract 12 days,then well get the the last day of the previous month:
How to order records by one month from today. I have tried the following but it does not work.
WHERE MONTH(date) = MONTH(CURDATE() -1 month)
Do you mean this? This will get you everything in your table where the month of the date field is equal to the current month - 1:
WHERE MONTH(date) = month(curdate() - interval 1 month)
WHERE MONTH(date) = MONTH(CURDATE() - INTERVAL 1 MONTH)
Hard to tell what you're after from your question, so I'm taking a punt that you want to show records that have the date that is exactly one month from now
SELECT *
FROM yourtable
WHERE MONTH(date) = MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
That will get all entries for October, if we are currently in November.
If you want it down to the day, so if today is November 20 you want to grab items from October 20, you'll need something like this
SELECT *
FROM agentjobsets
WHERE DATE(created_at) = DATE_SUB(CURDATE(), INTERVAL 1 MONTH)