I have the following stored procedure get the last 12 months from the current date in MySql
DELIMITER $$
CREATE
PROCEDURE `Calendar`()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS `Cal` (
`month` INT(11) ,
`year` INT(11)
);
TRUNCATE TABLE Cal;
SET #counter := -1;
WHILE (DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL #counter MONTH))) < DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL 12 MONTH)))) DO
INSERT INTO Cal SELECT DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL #counter:=#counter + 1 MONTH),'%m'),DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL #counter + 1 MONTH),'%Y');
END WHILE;
SELECT * FROM `Cal`;
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS Calendar;
The output table is
9 2017
10 2017
11 2017
12 2018
1 2018
2 2018
3 2018
4 2018
5 2018
6 2018
7 2018
8 2018
9 2018
In the month 12, the year is 2018. It should be 2017 and also the row 9 2017 shouldn't be there since its the 13th month including the current month. How can I make the 12th month's year to 2017 and remove the 9 2017 Thanks in advance
Here's a stored procedure that will give you the output you desire. It's longer but simpler than what you were attempting.
DELIMITER //
DROP PROCEDURE IF EXISTS Calendar //
CREATE PROCEDURE Calendar ()
BEGIN
DECLARE month INT DEFAULT MONTH(CURDATE());
DECLARE year INT DEFAULT YEAR(CURDATE()) - 1;
CREATE TEMPORARY TABLE CAL (month INT, year INT);
REPEAT
SET month = month + 1;
IF (month = 13) THEN
SET month = 1;
SET year = year + 1;
END IF;
INSERT INTO CAL VALUES (month, year);
UNTIL month = MONTH(CURDATE())
END REPEAT;
SELECT * FROM CAL;
DROP TABLE CAL;
END //
DELIMITER ;
CALL Calendar();
Output:
month year
10 2017
11 2017
12 2017
1 2018
2 2018
3 2018
4 2018
5 2018
6 2018
7 2018
8 2018
9 2018
You can also do it without a creating a stored procedure, temporary table, inserting into temporary table and dropping the stored procedure after
By using a number generator in MySQL code.
Query
SELECT
MONTH(NOW() - INTERVAL number_generator.number MONTH) AS `month`
, YEAR(NOW() - INTERVAL number_generator.number MONTH) AS `year`
FROM (
SELECT
(#NUMBER := #NUMBER + 1) AS number
FROM (
SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS records_1
CROSS JOIN (
SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS records_2
CROSS JOIN (SELECT #NUMBER := 0) AS init_user_params
) AS number_generator
WHERE
number_generator.number BETWEEN 1 AND 12
AND
MONTH(NOW()) <> MONTH(NOW() - INTERVAL number_generator.number MONTH)
ORDER BY
YEAR(NOW() - INTERVAL number_generator.number MONTH) ASC
, MONTH(NOW() - INTERVAL number_generator.number MONTH) ASC
Results
| month | year |
|-------|------|
| 10 | 2017 |
| 11 | 2017 |
| 12 | 2017 |
| 1 | 2018 |
| 2 | 2018 |
| 3 | 2018 |
| 4 | 2018 |
| 5 | 2018 |
| 6 | 2018 |
| 7 | 2018 |
| 8 | 2018 |
see demo http://sqlfiddle.com/#!9/340e01/584
Note if you use MySQL 8.0+ the syntax is more clear using common table expressions.
Query
WITH RECURSIVE number_generator (number) AS
(
SELECT 1 AS number
UNION ALL
SELECT number + 1 FROM number_generator WHERE number < 12
)
SELECT
MONTH(NOW() - INTERVAL number_generator.number MONTH) AS `month`
, YEAR(NOW() - INTERVAL number_generator.number MONTH) AS `year`
FROM
number_generator
WHERE
MONTH(NOW()) <> MONTH(NOW() - INTERVAL number_generator.number MONTH)
ORDER BY
YEAR(NOW() - INTERVAL number_generator.number MONTH) ASC
, MONTH(NOW() - INTERVAL number_generator.number MONTH) ASC
Results
| month | year |
|-------|------|
| 10 | 2017 |
| 11 | 2017 |
| 12 | 2017 |
| 1 | 2018 |
| 2 | 2018 |
| 3 | 2018 |
| 4 | 2018 |
| 5 | 2018 |
| 6 | 2018 |
| 7 | 2018 |
| 8 | 2018 |
see demo https://www.db-fiddle.com/f/chJ4qS2ocXS79vGkFBg7PR/13
Related
I have a table which contains the following data
userId | name | from
1 | aaa | 2020-09-23
2 | bbb | 2020-09-01
3 | ccc | 2019-05-12
4 | ddd | 2019-06-01
5 | eee | 2018-06-23
6 | fff | 2018-07-23
It is for an educational purpose and therefore the year runs from September to August rather than January to December. How do I output all of the users who were added since the previous September (so if it's October 2020 then 1 month ago, if it's January 2021 then 4 months ago)? The query has to be relative so that it always outputs the previous September to the time that the query is run rather than a specific September.
The result of the query should be
bbb 2020-09-01
aaa 2020-09-23
With NOT EXISTS:
select t.* from tablename t
where t.`from` >= concat(year(current_date), '-09-01')
and not exists (
select 1 from tablename
where name = t.name
and `from` between
concat(year(current_date), '-09-01') - interval 1 year
and
concat(year(current_date), '-09-01') - interval 1 day
)
Or maybe:
select t.* from tablename t
where t.`from` >= concat(year(current_date) - (month(current_date) < 9), '-09-01')
and not exists (
select 1 from tablename
where name = t.name
and `from` between
concat(year(current_date) - (month(current_date) < 9), '-09-01') - interval 1 year
and
concat(year(current_date) - (month(current_date) < 9), '-09-01') - interval 1 day
)
so if you execute the query in say March 2021, you will get the correct results that compare the current educational year with the previous one.
See the demo.
Results:
> userId | name | from
> -----: | :--- | :---------
> 1 | aaa | 2020-09-23
> 2 | bbb | 2020-09-01
You can subtract 9 months and compare to the year:
where year(`from` - interval 9 month) = year(curdate() - interval 9 month)
Actually, you might want year(curdate()) +/- 1 depending on how you are identifying the year.
I want a MySQL query that will fetch all Fridays with date for the year 2017.
I know that SQL query for the same is:
SELECT Fridays = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)
FROM (SELECT TOP 366 num = ROW_NUMBER() OVER(ORDER BY a.NAME)-1 FROM dbo.syscolumns a, dbo.syscolumns b) n
WHERE DATENAME(weekday, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)) = 'Friday'
I am looking for a MySQL alternative for the same.
This is an exact conversion of your sql logic in mysql
SELECT DATE_ADD(MAKEDATE(year(now()),1), INTERVAL #num:=#num+1 DAY)
From (select #num:=-1) num
where DAYNAME(DATE_ADD(MAKEDATE(year(now()),1), INTERVAL #num:=#num+1 DAY))="Friday"
limit 365
Mysql does not have row_number function until version 8. Prior to version 8 row_number can be simulated using variables.
select s.num , date_add(str_to_date(concat('2016','/','12','/','31'),'%Y/%m/%d'), interval s.num day) dt,
dayname(date_add(str_to_date(concat('2016','/','12','/','31'),'%Y/%m/%d'), interval s.num day)) dy
from
(
select #num:=#num + 1 num from information_schema.columns,(select #num:=0) n
limit 35
) s
where dayname(date_add(str_to_date(concat('2016','/','12','/','31'),'%Y/%m/%d'), interval s.num day)) = 'Friday';
information_schema.columns is similar to dbo.syscolumns.
------+------------+--------+
| num | dt | dy |
+------+------------+--------+
| 6 | 2017-01-06 | Friday |
| 13 | 2017-01-13 | Friday |
| 20 | 2017-01-20 | Friday |
| 27 | 2017-01-27 | Friday |
| 34 | 2017-02-03 | Friday |
+------+------------+--------+
5 rows in set, 2 warnings (0.26 sec)
SELECT * FROM table WHERE ([date] BETWEEN date_start and date_end) AND
DAYNAME([date])='Friday'
Source of the query: Get data for every friday between two dates
I have an small application which was build with CodeIgniter 3 and need to perform a report which will be converted to Chart.js. The report should be in yearly basis but at given specific date every month. The requirement are all data count must be from 4th to 3rd monthly. Like this:
For example January Report would be from 4th January to 3rd February, 4th February to 3rd March,... and so on.
I have created a MySQL query but I'm stuck on how to get the date too date. My Query are as follows:
SELECT DATE_FORMAT(odd_date_created, '%Y') as 'year',
DATE_FORMAT(odd_date_created, '%m') as 'month',
COUNT(odd_id) as 'total', status
FROM odd_data
WHERE status = $id and
GROUP BY DATE_FORMAT(odd_date_created, '%Y%m'), status
I'm new to MySQl. Could somebody help me on this. I'm stuck where should I put the date to date query.
Firstly I want to caution you not to use "between" with the following when you come to join your data, use this method instead data.date >= r.period_start_dt and data.date < r.period_end_dt
Secondly I am assuming your data does have dates or timestamps and that will fall between the calculated ranges that follow:
set #year :=2017;
select
*
from (
select
start_dt + INTERVAL m.n MONTH period_start_dt
, start_dt + INTERVAL m.n + 1 MONTH period_end_dt
from (
select str_to_date(concat(#year,'-01-04'),'%Y-%m-%d') start_dt ) seed
cross join (select 0 n union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11
) m
) r
## LEFT JOIN YOUR DATA
## ON data.date >= r.period_start_dt and data.date < r.period_end_dt
Example ranges: (produce you own at this demo: http://rextester.com/CHTKSA95303 )
nb dd.mm.yyyy (.de format)
+----+---------------------+---------------------+
| | period_start_dt | period_end_dt |
+----+---------------------+---------------------+
| 1 | 04.01.2017 00:00:00 | 04.02.2017 00:00:00 |
| 2 | 04.02.2017 00:00:00 | 04.03.2017 00:00:00 |
| 3 | 04.03.2017 00:00:00 | 04.04.2017 00:00:00 |
| 4 | 04.04.2017 00:00:00 | 04.05.2017 00:00:00 |
| 5 | 04.05.2017 00:00:00 | 04.06.2017 00:00:00 |
| 6 | 04.06.2017 00:00:00 | 04.07.2017 00:00:00 |
| 7 | 04.07.2017 00:00:00 | 04.08.2017 00:00:00 |
| 8 | 04.08.2017 00:00:00 | 04.09.2017 00:00:00 |
| 9 | 04.09.2017 00:00:00 | 04.10.2017 00:00:00 |
| 10 | 04.10.2017 00:00:00 | 04.11.2017 00:00:00 |
| 11 | 04.11.2017 00:00:00 | 04.12.2017 00:00:00 |
| 12 | 04.12.2017 00:00:00 | 04.01.2018 00:00:00 |
+----+---------------------+---------------------+
Given the specification, I think I would tempted to cheat it... subtract 3 days from the date. Doing that, Jan 4 backs up to Jan 1, Feb 3 backs up to Jan 31... so those all end up as January.
SELECT DATE_FORMAT(odd_date_created + INTERVAL -3 DAY, '%Y') AS `year`
, DATE_FORMAT(odd_date_created + INTERVAL -3 DAY, '%m') AS `month`
, ...
FROM ...
GROUP
BY DATE_FORMAT(odd_date_created + INTERVAL -3 DAY, '%Y')
, DATE_FORMAT(odd_date_created + INTERVAL -3 DAY, '%m')
This falls apart if there's oddball ranges... if it's not always the 4th and 3rd.
What query to get all the Sundays date between January - March 2016?
so output just the shown date only.
| Sundays |
|- - - - - -|
| 2 |
| 9 |
| 16 |
| 23 |
| 30 |
- - - - - -
i find query like this select ('2013-04-15' - interval dayofweek('2013-08-15') - 1 day) - interval (weekofyear('2013-04-15') - 1) * 7 day as SUNDAY;
but just shown one sunday.
To get the Sunday below query will work for you
select DATE_ADD('2016-01-01', INTERVAL ROW DAY) as Date,
row+1 as DayOfMonth
from
(
SELECT #row := #row + 1 as row
FROM
(
select 0
union all
select 1
union all
select 3
union all
select 4
union all
select 5
union all
select 6
) t1,
(
select 0
union all
select 1
union all
select 3
union all
select 4
union all
select 5
union all
select 6
) t2,
(SELECT #row:=-1
) t3 limit 31
) b
where
DATE_ADD('2016-01-01', INTERVAL ROW DAY)
between '2016-01-01' and '2016-03-31'
and
DAYOFWEEK(DATE_ADD('2016-01-01', INTERVAL ROW DAY))=1;
Can Any one please tell me how to add one whole month date's in mysql database. For now I'm adding one date at a time.
This query generates all the days of a given month.
I found this in this post.
When you change NOW() function with any date you want, it generates the dates of that month. You can also join this with your queries or you can use it with INSERT [(col_name,...)] SELECT ... statement to insert all the dates to a table.
SELECT date_field
FROM
(
SELECT
MAKEDATE(YEAR(NOW()),1) +
INTERVAL (MONTH(NOW())-1) MONTH +
INTERVAL daynum DAY date_field
FROM
(
SELECT t*10+u daynum
FROM
(SELECT 0 t UNION SELECT 1 UNION SELECT 2 UNION SELECT 3) A,
(SELECT 0 u UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9) B
ORDER BY daynum
) AA
) AAA
WHERE MONTH(date_field) = MONTH(NOW());
plan
build up decimal numbers with cross join on digits - all numbers can be expressed as :
an10^n + .. a0*10^0
use date_add to add start of month data filtering where less than next month start
setup
create table example
(
`date` date primary key not null
);
drop view if exists digits_v;
create view digits_v
as
select 0 as n
union all
select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all
select 7 union all select 8 union all select 9
;
query
insert into example ( `date` )
select date_add(date('2015-11-01'), interval a2.n * 100 + a1.n * 10 + a0.n day)
from digits_v a2
cross join digits_v a1
cross join digits_v a0
where date_add(date('2015-11-01'), interval a2.n * 100 + a1.n * 10 + a0.n day)
< date('2015-12-01')
;
sqlfiddle
selecting the top 10 gives
select `date`
from example
order by `date`
limit 10
;
.
+----------------------------+
| date |
+----------------------------+
| November, 01 2015 00:00:00 |
| November, 02 2015 00:00:00 |
| November, 03 2015 00:00:00 |
| November, 04 2015 00:00:00 |
| November, 05 2015 00:00:00 |
| November, 06 2015 00:00:00 |
| November, 07 2015 00:00:00 |
| November, 08 2015 00:00:00 |
| November, 09 2015 00:00:00 |
| November, 10 2015 00:00:00 |
+----------------------------+