I have a date type column in mysql table. I need to order the date starting from the current month independent of year.
tblEvent
event_date
__________
2016-01-07
2016-01-13
2016-05-16
2016-05-22
2016-05-30
2018-06-21
2016-06-23
2016-07-14
2018-08-16
2016-10-20
1990-12-09
2016-12-15
As the current month is Jun therefore, the required Output should be:
event_date
__________
2018-06-21
2016-06-23
2016-07-14
2018-08-16
2016-10-20
1990-12-09
2016-12-15
2016-01-07
2016-01-13
2016-05-16
2016-05-22
2016-05-30
Order must start from the current month
month
for example if the current month is Nov then the order must be
Nov, Dec, Jan, Feb, Mar, ....., Oct
also the respective dates must be in asc order.
order must be independent of year(i.e. no matter what year is there
in the date)
SELECT event_date
FROM tblEvent
ORDER BY
IF(MONTH(event_date) < MONTH(NOW()), MONTH(event_date) + 12, MONTH(event_date)),
DAY(event_date)
Info:
Date and Time Functions
Control Flow Functions
If I understand your question correctly, a soultion can be like this:
select
event_date
from
tblevent
order by
(12+month(event_date)-month(current_date)) mod 12,
day(event_date)
this query will sort all rows starting from the current month, ordered by month and day not considering the year. Please see a working fiddle here.
I just try below query, Getting all records, starts from current month in desc order. like:- month 6, 5, 4, 3, 2, 1
SELECT *
FROM `schedules`
WHERE date_format( `ScheduleDate` , "%m" ) <= date_format( NOW( ) , "%m" )
ORDER BY `ScheduleDate` DESC
LIMIT 0 , 30;
You can try in this way, Hope this will help.
Related
I want to do other 2 columns that shows me, respectively, the first date and the last day of the week grouped.
Currently, I'm summarizing a query by week period. When I run this query, it shows me the results on table:
SELECT
pc.date,
CONCAT(YEAR(pc.date), '/', WEEK(pc.date)) as year_week
FROM pc
GROUP BY CONCAT(YEAR(pc.date), '/', WEEK(pc.date))
ORDER BY pc.date
date
year_week
2020-09-02
2020/35
2020-09-07
2020/36
2020-09-17
2020/37
2020-09-23
2020/38
2020-09-28
2020/39
2020-10-10
2020/40
2020-10-11
2020/41
2020-10-21
2020/42
2020-10-28
2020/43
How can I find the first and last day of grouped week?
You can use the WEEKDAY function. Demo:
select
date_add(dt, interval -WEEKDAY(dt)-1 day ) FirstDayOfWeek,
date_add(date_add(dt, interval -WEEKDAY(dt)-1 day), interval 6 day) LastDayOfWeek,
week(dt) wk
from (
select '2020-09-02' dt union all
select '2020-09-07' union all
select '2020-09-17'
) t
Returns
FirstDayOfWeek LastDayOfWeek wk
2020-08-30 2020-09-05 35
2020-09-06 2020-09-12 36
2020-09-13 2020-09-19 37
I have a table event, where i have records with a field end_date, so my problem is i want to fetch number of records, grouping month wise, where end_date should with in that month only, so for example:
If a record have end_date as 2013-01-01 00:00:00 then it should be counted in January 2013, and i am not able to do that. I am unable to put that where condition, how to do tell database that end_date should be between the month for which it is currently grouping.
SELECT COUNT(*) AS 'count', MONTH(created) AS 'month', YEAR(created) AS 'year' FROM event WHERE is_approved =1 GROUP BY YEAR(created), MONTH(created)
Please help me out.
EDIT :
Data say i have is like:
Record name end_date
record_1 2013-11-01 00:00:00
record_2 2013-11-30 00:00:00
record_3 2013-12-01 00:00:00
record_4 2013-12-04 00:00:00
record_5 2013-12-06 00:00:00
record_6 2013-12-10 00:00:00
...many more
Result Expected is:
Count month year
2 11 2013
4 12 2013
....so on
Try this:
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1
GROUP BY EXTRACT(YEAR_MONTH FROM end_date);
OR
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1
GROUP BY YEAR(end_date), MONTH(end_date);
::EDIT::
1. end date is greater than that particular month - Simply add where condition in your query and pass particular month in format of YYYYMM instead of 201411
2. event is started - Add one more where condition to check whether the created date is less then current date
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1 AND
EXTRACT(YEAR_MONTH FROM end_date) > 201411 AND
DATE(created) <= CURRENT_DATE()
GROUP BY EXTRACT(YEAR_MONTH FROM end_date);
OR
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1 AND
EXTRACT(YEAR_MONTH FROM end_date) > 201411 AND
DATE(created) <= CURRENT_DATE()
GROUP BY YEAR(end_date), MONTH(end_date);
The count is aggregated based on the month and year so if you are spanning years, you wont have Jan 2013 mixed with Jan 2014, hence pulling those values too and that is the same basis of the group by.
As for your criteria, that all goes in the WHERE clause. In this case, I did anything starting with Jan 1, 2013 and ending Dec 31, 2014 via 'yyyy-mm-dd' standard date recognized format. That said, and the structure of the table you provided, I am using the "end_date" column.
SELECT
YEAR(end_date) AS EventYear,
MONTH(end_Date) AS EventMonth,
COUNT(*) AS EventCount
FROM
event
WHERE is_approved = 1
and end_date between '2013-01-01' and '2014-12-31'
GROUP BY
YEAR(end_date),
MONTH(end_Date)
Now, if you want them to have the most recent events on the top, I would put the year and month descending so 2014 is listed first, then 2013, etc and months within them as December (month 12), before the others.
GROUP BY
YEAR(end_date) DESC,
MONTH(end_Date) DESC
Your criteria could be almost anything from as simple as just a date change, approved status, or even get counts per account status is so needed, such as (and these are just EXAMPLES if you had such code status values)
SUM( is_approved = 0 ) as PendingEvent,
SUM( is_approved = 1 ) as ApprovedEvent,
SUM( is_approved = 2 ) as CancelledEvent
Per comment feedback.
For different date ranges, ignore the between clause and change the WHERE to something like
WHERE end_date > '2014-08-01' or all after a date...
where end_date < '2014-01-01' or all before a date...
They will still group by month / year. If you wanted based on a start date of the event, just change that column in instead, or do IN ADDITION to the others.
MySQL has a bunch of date and time functions that can help you with that. For example:
MONTH() Return the month from the date passed
or
YEAR() Return the year
So you can just get the month and year of your dates. And group your results by them.
SELECT
COUNT(*) cnt
,MONTH(end_date) month
,YEAR(end_date) year
FROM events
GROUP BY month, year
Result :
cnt month year
2 11 2013
4 12 2013
Update:
For filtering only the records that have an end_date greater than a particular month AND have already started, you just need to add a WHERE clause. For example, if the particular month were February 2015:
SELECT
COUNT(*) cnt
,MONTH(end_date) month
,YEAR(end_date) year
FROM events
WHERE end_date >= '2015-03-01'
AND created < NOW()
GROUP BY month, year
Alternatively, the first part of the WHERE clause can be rewritten in the following way, which is probably more comfortable to use if you have to pass the year and month as distinct parameters.
...
WHERE (YEAR(end_date) > 2015
OR (YEAR(end_date) = 2015 AND MONTH(end_date) > 02))
AND created...
SELECT COUNT(*) AS 'count', MONTH(created) AS 'month', YEAR(created) AS 'year' FROM event WHERE is_approved =1 and month(created) = "the month u want" and year(created) = "the year you want" group by GROUP BY YEAR(created), MONTH(created)
you will need to pull the month and year... i could help with that but not sure how you are getting it but months would be 01/02/03 ect and year is 2013/2014/2015 ect
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();
Below is my mysql table:
--------------------------
ID Date_From Date_To
--------------------------
1 2011-02-01 2011-02-28
2 2012-09-01 2012-09-30
3 2012-10-01 2012-10-30
4 2012-11-01 2012-11-30
5 2012-12-01 2012-12-30
6 2013-01-01 2013-01-30
7 2014-03-01 2014-03-31
I have this mysql statement:
SELECT *
FROM TIME_PERIOD
WHERE
(YEAR(DATE_FROM) >= '2012' AND MONTH(DATE_FROM) >= '10')
AND (YEAR(DATE_TO) <= '2013' AND MONTH(DATE_TO) <= '12');
which returns only the records with ID 3, 4, and 5. What Im expecting to return are records with ID 3, 4, 5, and 6.
Please help.
You need to use query like this:
SELECT * FROM TIME_PERIOD WHERE
(YEAR(DATE_FROM) >= '2013' OR (YEAR(DATE_FROM) >= '2012' AND
MONTH(DATE_FROM) >= '10')) AND
(YEAR(DATE_TO) <= '2013' AND MONTH(DATE_TO) <= '12');
Month seems to be 01 in record 6. It wont return that because MONTH(DATE_FROM) >= '10'
In id 6 the from date is 2013-01-01 and it fails in 2013-01-01 as the month is 01 which is less than 10. As you have mentioned all and condition so any false will not include that record.
You can create a separate column as year_month like YYYYMM and create two triggers to automatically update the column ON INSERT and ON UPDATE if required.
Then the query would be as simple as
WHERE year_month >= 201210 and year_month <= 201312
You can use this command to get your result ...
SELECT * FROM `TIME_PERIOD` WHERE `DATE_FROM` >= '2012-10-01' AND `DATE_TO` <= '2013-01-30'
I have one table (but at many locations):
DATE STUFF
-------------------
2011-12-01 DATA
2011-12-02 DATA
2011-12-03 DATA
...
2011-12-31 DATA
2012-01-01 DATA
2012-01-02 DATA
My table covers multiple years from 2005 to 2012. I want to get AGGREGATE Function values, i.e., SUM/AVG/MAX/MIN, for each month within each year. Easy:
GROUP BY DATE_FORMAT(DATE, '%Y'), DATE_FORMAT(DATE, '%m')
I want go do the same for 3-month time periods within those years... this works for all but one:
GROUP BY DATE_FORMAT(DATE, '%Y'), DATE_FORMAT(DATE, '%m') IN (12, 01, 02)
My other time periods work, because they are in the same year (03, 04, 05), (06, 07, 08), and (09, 10, 11)... but the GROUP BY above is grouping December of 2012 with January/February of 2012. My time period has to be December 2011 and January/February 2012, December 2010 and January/February 2011... ...
I want to keep this generic, so I don't have to update with date spans, in order to put the code in a stored procedure for multiple locations.
I've tried to join the table to it self by shifting the year ahead by one if MONTH 12. This yielded undesirable results.
GROUP BY floor((month(DATE) + year(DATE) * 12 -1 + %month_shift%) / %month_group_period%)
where %month_group_period% is three (3) in your example
and %month_shift% is one (1) to obtain december, january, february together, and so on
EDIT: this works for 5 month period too (if you want)
I'm assuming a little bit here about what you really want to do with DATE_FORMAT(DATE, '%m') IN (12, 01, 02), but:
SELECT IF(DATE_FORMAT(DATE, '%m') = 12, DATE_FORMAT(DATE, '%Y') + 1, DATE_FORMAT(DATE, '%Y')) AS yr,
CASE DATE_FORMAT(DATE, '%m')
WHEN 12 THEN 1
WHEN 1 THEN 1
WHEN 2 THEN 1
WHEN 3 THEN 2
WHEN 4 THEN 2
WHEN 5 THEN 2
WHEN 6 THEN 3
WHEN 7 THEN 3
WHEN 8 THEN 3
WHEN 9 THEN 4
WHEN 10 THEN 4
WHEN 11 THEN 4
END AS qtr
FROM ...
GROUP BY IF(DATE_FORMAT(DATE, '%m') = 12, DATE_FORMAT(DATE, '%Y') + 1, DATE_FORMAT(DATE, '%Y')),
CASE DATE_FORMAT(DATE, '%m')
WHEN 12 THEN 1
WHEN 1 THEN 1
WHEN 2 THEN 1
WHEN 3 THEN 2
WHEN 4 THEN 2
WHEN 5 THEN 2
WHEN 6 THEN 3
WHEN 7 THEN 3
WHEN 8 THEN 3
WHEN 9 THEN 4
WHEN 10 THEN 4
WHEN 11 THEN 4
END
I don't think you want to use GROUP BY how you are trying to do it. You would probably want to write your queries like this
SELECT MONTH(date) as `month`, SUM(stuff) as `sum`
FROM table
WHERE YEAR(date) IN ('2010', '2011', 2012')
GROUP BY `month`
The GROUP BY clause is not where you should be filtering/formatting your data.
Of course, you could use whatever aggregate function you want instead of SUM. Also you can omit the WHERE clause for all years, or modify the years to be included as needed.
If you want year and month, you could simply modify the query like this:
SELECT YEAR(date) as `year`, MONTH(date) as `month`, SUM(stuff) as `sum`
FROM table
WHERE `year` IN ('2010', '2011', 2012')
GROUP BY `year`, `month`
You need to subtract one month from the date for the group by:
GROUP BY DATE_FORMAT(date_add(DATE, interval 1 month), '%Y'),
DATE_FORMAT(date_add(date, interval 1 month), '%m') IN (12, 01, 02)
You will probably also have to modify the select.
select DATE_FORMAT(date_add(date_add(DATE, interval 1 month), interval -1 month), '%Y'),
DATE_FORMAT(date_add(date_add(date, interval 1 month), interval -1 month), '%m')
I would try something like this:
GROUP BY DATE_FORMAT(IF(MONTH(`DATE`)=12,DATE_ADD(`DATE`,INTERVAL 1 YEAR),`DATE`),'%Y')
, DATE_FORMAT(`DATE`,'%m')
Basically, if month is 12, then add 1 year to it.
Actually, I would tend to do it something like this:
GROUP BY DATE_FORMAT(DATE_ADD(`DATE`,INTERVAL IF(MONTH(`DATE`)=12,1,0) YEAR),'%Y')
, CASE WHEN MONTH(`DATE`) IN (12,1,2) THEN 1
WHEN MONTH(`DATE`) IN (3,4,5) THEN 2
WHEN MONTH(`DATE`) IN (6,7,8) THEN 3
WHEN MONTH(`DATE`) IN (9,10,11) THEN 4
END