From here, I modified the query slightly to get
select adddate('2015-02-01', numlist.id) as `my_date`,
weekday(adddate('2015-02-01', numlist.id)) as day_no,
dayname(adddate('2015-02-01', numlist.id)) as `day_name`
from
(SELECT n1.i + n10.i*10 + n100.i*100 AS id
FROM num n1 cross join num as n10 cross join num as n100) as numlist
where adddate('2015-02-01', numlist.id) <= '2015-02-28'
and dayname(adddate('2015-02-01', numlist.id)) in( 'Monday', 'Tuesday');
which gives
+------------+--------+----------+
| my_date | day_no | day_name |
+------------+--------+----------+
| 2015-02-02 | 0 | Monday |
| 2015-02-03 | 1 | Tuesday |
| 2015-02-09 | 0 | Monday |
| 2015-02-10 | 1 | Tuesday |
| 2015-02-16 | 0 | Monday |
| 2015-02-17 | 1 | Tuesday |
| 2015-02-23 | 0 | Monday |
| 2015-02-24 | 1 | Tuesday |
+------------+--------+----------+
8 rows in set (0.01 sec)
or from here
select * from
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date from
(select 0 t0 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) t0,
(select 0 t1 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) t1,
(select 0 t2 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) t2,
(select 0 t3 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) t3,
(select 0 t4 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) t4) v
where selected_date between '2015-02-01' and '2015-02-28'
and dayname(selected_date) in ('Monday', 'Tuesday');
result
+---------------+
| selected_date |
+---------------+
| 2015-02-02 |
| 2015-02-03 |
| 2015-02-09 |
| 2015-02-10 |
| 2015-02-16 |
| 2015-02-17 |
| 2015-02-23 |
| 2015-02-24 |
+---------------+
8 rows in set (0.23 sec)
CREATE TABLE mydate( blah date);
INSERT INTO mydate (blah)
select adddate('2015-02-01', numlist.id) as `my_date`
-- weekday(adddate('2015-02-01', numlist.id)) as day_no,
-- dayname(adddate('2015-02-01', numlist.id)) as `day_name`
from
(SELECT n1.i + n10.i*10 + n100.i*100 AS id
FROM num n1 cross join num as n10 cross join num as n100) as numlist
where adddate('2015-02-01', numlist.id) <= '2015-05-28'
and dayname(adddate('2015-02-01', numlist.id)) in( 'Monday', 'Tuesday');
(need a table num)
mysql> select * from mydate;
+------------+
| blah |
+------------+
| 2015-02-02 |
| 2015-02-03 |
| 2015-02-09 |
| 2015-02-10 |
| 2015-02-16 |
| 2015-02-17 |
| 2015-02-23 |
| 2015-02-24 |
| 2015-03-02 |
OR
insert into mydate (blah)
select * from
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date from
(select 0 t0 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) t0,
(select 0 t1 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) t1,
(select 0 t2 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) t2,
(select 0 t3 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) t3,
(select 0 t4 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) t4) v
where selected_date between '2015-02-01' and '2015-05-28'
and dayname(selected_date) in ('Monday', 'Tuesday');
No table necessary. Same result as above.
You can use MAKEDATE()
, it makes the day_of_year
to a date.
Test:
mysql> SELECT * FROM cli_table;
+-------+--------------+-------------+-------+
| month | day_of_month | day_of_year | value |
+-------+--------------+-------------+-------+
| 1 | 1 | 1 | 23.00 |
| 1 | 2 | 2 | 22.95 |
| 12 | 31 | 365 | 23.12 |
+-------+--------------+-------------+-------+
3 rows in set (0.00 sec)
mysql> SELECT
-> MAKEDATE(YEAR(CURDATE()),day_of_year) AS Date,
-> value AS DayValue
-> FROM test.cli_table;
+------------+----------+
| Date | DayValue |
+------------+----------+
| 2015-01-01 | 23.00 |
| 2015-01-02 | 22.95 |
| 2015-12-31 | 23.12 |
+------------+----------+
3 rows in set (0.00 sec)
mysql>
You can try it in this Fiddle.
EDIT:
According of what you want, you can make it with pure SQL:
mysql> SELECT * FROM test.cli_table;
+-------+--------------+-------------+-------+
| month | day_of_month | day_of_year | value |
+-------+--------------+-------------+-------+
| 1 | 1 | 1 | 5.00 |
| 1 | 2 | 2 | 6.00 |
| 1 | 3 | 3 | 7.00 |
| 12 | 25 | 359 | 23.00 |
| 12 | 26 | 360 | 22.95 |
| 12 | 27 | 361 | 1.00 |
| 12 | 28 | 362 | 2.00 |
| 12 | 29 | 363 | 3.00 |
| 12 | 30 | 364 | 4.00 |
| 12 | 31 | 365 | 23.12 |
+-------+--------------+-------------+-------+
10 rows in set (0.00 sec)
mysql> SELECT
-> v.selected_date,
-> v.dayyear,
-> c.value AS DayValue,
-> c.day_of_year
-> FROM test.cli_table AS c
-> JOIN (SELECT
-> adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) AS selected_date,
-> DAYOFYEAR(adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i)) AS dayyear
-> FROM
-> (select 0 i 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) t0,
-> (select 0 i 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) t1,
-> (select 0 i 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) t2,
-> (select 0 i 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) t3,
-> (select 0 i 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) t4) AS v
-> ON (v.selected_date between '2015-12-25' AND '2016-01-03'
-> AND v.dayyear=c.day_of_year);
+---------------+---------+----------+-------------+
| selected_date | dayyear | DayValue | day_of_year |
+---------------+---------+----------+-------------+
| 2015-12-25 | 359 | 23.00 | 359 |
| 2015-12-26 | 360 | 22.95 | 360 |
| 2015-12-27 | 361 | 1.00 | 361 |
| 2015-12-28 | 362 | 2.00 | 362 |
| 2015-12-29 | 363 | 3.00 | 363 |
| 2015-12-30 | 364 | 4.00 | 364 |
| 2015-12-31 | 365 | 23.12 | 365 |
| 2016-01-01 | 1 | 5.00 | 1 |
| 2016-01-02 | 2 | 6.00 | 2 |
| 2016-01-03 | 3 | 7.00 | 3 |
+---------------+---------+----------+-------------+
10 rows in set (0.00 sec)
mysql>
Query Meaning:
SELECT
v.selected_date,
v.dayyear,
c.value AS DayValue,
c.day_of_year
FROM test.cli_table AS c
--- > This JOIN is generating all the dates between the selected_dates ('2015-12-25' AND '2016-01-03')
JOIN (SELECT
adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) AS selected_date,
DAYOFYEAR(adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i)) AS dayyear
FROM
(select 0 i 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) t0,
(select 0 i 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) t1,
(select 0 i 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) t2,
(select 0 i 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) t3,
(select 0 i 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) t4) AS v
--- > Here I'm matching every dayyear of the generated dates with the cli_table.day_of_year.
ON (v.selected_date between '2015-12-25' AND '2016-01-03'
AND v.dayyear=c.day_of_year);
Best Answer
First, let's use MySQL's format for dates:
Now we need to combine the '2016' (or 2016-1) from the Claim date to the '04-27' of some Start date. It does not matter whether it is the original or current "start date". To decide on the "-1", we need to compare both mm-dd values, which can be done as simple string.
In the following code, I am assuming
DATE
datatype was used; using string functions works fine. For example:Here's the expression to generate '2015-04-27', which can be used as a
DATE
or inDATE_FORMAT()
to format it as desired:I thought about using julian dates, but leapday messes up things.