Mysql select rows based on previous quarterly month from current month - mysql

I want to query row to view registered record based on previous quarterly month, for example:
current month / year: Jan 2018
the query should show all records by registered month which is: Oct 2017, Jul 2017, Apr 2017 and so on.
I use below query but only can select rows for last 3 month.
SELECT name, date, amount, agreement, bank
FROM `account`
WHERE (YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH) AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH))

You can do arithmetic like this:
select a.*
from account a
where mod(month(date), 3) = mod(month(current_date), 3);
You seem to understand how to handle the year component, but something like:
and date >= curdate() - interval 1 year

Try This One, Manually
select *
from account
Where current_date >= dateadd(year, -1, Getdate())
And Month(current_date) In (
Month(Getdate()),
(Case When Month(Getdate())+3 > 12 Then Month(Getdate())+3-12 Else Month(Getdate())+3 End),
(Case When Month(Getdate())+6 > 12 Then Month(Getdate())+6-12 Else Month(Getdate())+6 End),
(Case When Month(Getdate())+9 > 12 Then Month(Getdate())+9-12 Else Month(Getdate())+9 End)
)

Related

how to get weeklytotal and yesterday record in mysql in one table

Hi Everyone i am trying to implement query to get weekly and yesterday data in same table,
dummy output i have shared below, if yesterday not exist as per employee_id it should we showing 0 also as per my table week start from monday and end at sunday.please help me out how to query this get weekly_total and yesterday record and one table.
Table name-dailydata-
Sample data
employee_id
date
total
20
2022-04-25
10
20
2022-04-26
20
20
2022-04-27
20
20
2022-04-28
10
20
2022-04-29
20
20
2022-04-30
30
20
2022-04-31
40
20
2022-05-01
50
40
2022-04-26
20
expected output
employee_id
weekly_total
yesterday_record
20
200
40
40
20
0
mysql query to get weekly data
select employee_id,sum(total) as week_total from dailydata where date between '2022-04-25' and '2022-05-01'
You can try to use the condition aggregate function to make it.
We might add non-aggregate columns in the group by when we are using aggregate functions.
select employee_id,
SUM(total) as week_total,
SUM(CASE WHEN DATEDIFF('2022-05-01',date) = 1 THEN total ELSE 0 END) yesterday_record
from dailydata t1
where date between '2022-04-25' and '2022-05-01'
GROUP BY employee_id
You can use a case in the query to get yesterdays data, as long as the where does not exclude it, which is the case in the second query.
Once you have understood the principal you can define the date range so that it is calculated dynamically when you run the script if what you want is to see yesterday's figure and the last 7 days total.
You can also get yesterday using SUBDATE(NOW(),1) which is shorter.
select
employee_id,
sum(total) as week_total ,
sum(case when date = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
then total else 0 end as yesterday
from dailydata
where date between
DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
and DATE_SUB(CURDATE(), INTERVAL 1 DAY) ;
select
employee_id,
sum(total) as week_total ,
sum(case when date = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
then total else 0 end as yesterday
from dailydata
where date between '2022-04-25' and '2022-05-01';
Hope this may help you, You just need to use the aggregate function in the case of IFNULL.
DBFiddle URL: Click Here
For the start of the week
SELECT SUBDATE(CURDATE(), weekday(CURDATE())); --Start of week
For the end of the week
SELECT DATE(CURDATE() + INTERVAL (6 - WEEKDAY(CURDATE())) DAY); --End of week
Hereby SQL query for getting employe wise total and yesterday total. If yesterday's total doesn't exist so for that Have used IFNULL. Just used SUBDATE for getting the start and end of the week date by passing current date.
SELECT employee_id,
IFNULL(SUM(total),0) AS total,
IFNULL(SUM(CASE date WHEN subdate(CURDATE(), 1) THEN total ELSE 0 END),0) AS yesterday_total
FROM dailydata
WHERE date BETWEEN
SUBDATE(CURDATE(), weekday(CURDATE())) AND (CURDATE() + INTERVAL (6 - WEEKDAY(CURDATE())) DAY)
GROUP BY employee_id
Start of this week:
SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)
End of this week:
SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 6)
Yesterday:
select GETDATE() -1
Your query:
select
employee_id,
sum(total) as week_total,
(select sum(total) as week_total
from dailydata b
where [date] = convert(date, getdate() -1 )
and a.employee_id = b.employee_id) as yesterday_record
from
dailydata a
where
[date] between dateadd(wk, datediff(wk, 0, getdate()), 0) and dateadd(wk, datediff(wk, 0, getdate()), 6)
group by
employee_id

sql query add 1 year, less days passed in month

I have a table with rows of dates,
I need to get rows for one year:
SELECT DATE_FORMAT(cal_date, '%e %M') as cal_date, price_client,
price_owner, description FROM table WHERE cal_date <
DATE_ADD('2017-09-13' , INTERVAL 1 year)
The result of this query are date rows with dates from 1 September 2017 until 12 September 2018.
I would like the result from 13 September 2017 until 12 September 2018.
Updated, This is the correct query, works like I wanted:
WHERE cal_date >= '2017-09-13' and cal_date < DATE_ADD('2017-09-13' ,
INTERVAL 1 year)
for a generic format based on curdate() you could use
SELECT
DATE_FORMAT(cal_date, '%e %M') as cal_date
, price_client
, price_owner
, description
FROM table WHERE cal_date <
DATE_ADD(DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), interval 30 day), interval 1 day) ,
INTERVAL 1 year)
Seems to work once you account for cal_date>= same date
you handled < future date but you didn't handle > or >= current date. As it stands you will always return all records older than 1 year from today. you have no lower boundary defined.
DEMO: http://rextester.com/WDXHE5921
SELECT DATE_FORMAT(cal_date, '%e %M%Y') as cal_date, price_client,
price_owner, description
FROM table
WHERE cal_date < DATE_ADD('2017-09-13' , INTERVAL 1 year)
and cal_date >= '2017-09-13';
This question would have benefited from talking to the duck first

How do i select all records for the next 3 months including the current one when it turns into a new year

I am trying to select every record between current month and the next 2 months but I am not able to because the year will be changing from 2016 to 2017.
For ex.
I want to get all the records from November 2016 to January 2017.
The current query (shown below) i have has worked fine until this month because November 2016 + 2 months = Jan 2017.
select * from dateTable
where month(t2.`END_DATE`) between month(curdate()) and
month(DATE_ADD(curdate(), INTERVAL 2 MONTH))
and year(t2.`END_DATE`) = year(curdate());
This returns 0 rows because this cannot handle having two years, 2016 and 2017.
How would I go about doing this?
This should be what you need, although there are probably a number of ways of doing this
select * from dateTable
where `END_DATE` BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01')
AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 2 MONTH))
The result of this query will demonstrate the dates being generated
SELECT DATE_FORMAT(NOW() ,'%Y-%m-01') as from_date,
LAST_DAY(DATE_ADD(NOW(), INTERVAL 2 MONTH)) as to_date
Today this will generate
from_date to_date
2016-11-01 2017-01-31
Try to use full datetime with DATE_ADD and DATE_DIFF functions.
select * from dateTable where t2.`END_DATE`
between DATEADD(month,
DATEDIFF(month, 0, getdate() -- get difference to first day
), 0)
and DATEADD(month, 2, -- add 3 months interval to get first day of third
DATEADD(month,
DATEDIFF(month, 0, getdate() -- get difference to first day
), 0))
If you have to remove the first day from last month use DATEADD again and remove 1 second to get 23:59:59

Mysql date_sub interval 12 month

I'm trying to get all posts from the 12 last month, group by month. I have a quite correct query:
SELECT MONTH(time) as mois, YEAR(time) as annee, count(*) as nbre
FROM touist_stories
WHERE time >= DATE_SUB(now() + INTERVAL 1 MONTH, INTERVAL 2 YEAR)
group by MONTH(time)
order by YEAR(time) DESC, MONTH(time) DESC
But one month is always missing : november 2012
I tryied to add
+ INTERVAL 1 MONTH
to now() but it still missing... How can I get the 12 last month and not the 11 ones please?
Thanks
To get one year ago, here's a technique I've used in the past. Using #mysql variables, create a date based on the first day of a given month/year (via now()), then subtract 12 months. This example will get from Oct 1, 2012 to current -- which will include current Oct 2013. To exclude that, just add to where clause where I re-added 1 year so it goes from Oct 1, 2012 at 12:00:00 am to LESS THEN Oct 1, 2013 12:00:00.
SELECT
MONTH(time) as mois,
YEAR(time) as annee,
count(*) as nbre
FROM
touist_stories,
( select #lastYear := date_add( DATE_FORMAT(NOW(),
'%Y-%m-01'), interval -11 month) ) sqlvar
WHERE
time >= #lastYear
group by
MONTH(time)
order by
YEAR(time) DESC,
MONTH(time) DESC
Revised to make it go 11 months back (to November per example), and include UP TO AND INCLUDING all Current October activity.
For realy want on year data use 11 MONTH not 12
SELECT time
FROM touist_stories
WHERE time
BETWEEN
date_sub(Now(), INTERVAL 11 MONTH)
AND
Now();

MySQL - order by one month from today

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)