Replacement of values by logical condition by groups in SQL Server - sql

Here's my sample data
shop_code product_id doc_date ship_count mark_1 outputer y
-----------------------------------------------------------------------------
1 00664НСК 11628 2015-01-03 00:00:00.000 12 1 8 1
2 00664НСК 11628 2015-01-05 00:00:00.000 7 1 8 1
3 00664НСК 11628 2015-01-06 00:00:00.000 24 0 8 1
4 00664НСК 11628 2015-01-07 00:00:00.000 18 1 8 1
5 00664НСК 11628 2015-01-08 00:00:00.000 12 1 8 1
6 00664НСК 11628 2015-01-09 00:00:00.000 18 0 8 1
7 00664НСК 11628 2015-01-10 00:00:00.000 6 0 6 1
8 00664НСК 11628 2015-01-11 00:00:00.000 6 1 6 1
9 00664НСК 11628 2015-01-12 00:00:00.000 6 1 6 1
10 00664НСК 11628 2015-01-13 00:00:00.000 18 1 12 0
11 00664НСК 11628 2015-01-14 00:00:00.000 6 1 6 0
12 00664НСК 11628 2015-01-15 00:00:00.000 18 1 12 0
13 00664НСК 11628 2015-01-16 00:00:00.000 12 1 12 1
14 00664НСК 11628 2015-01-17 00:00:00.000 18 1 12 1
15 00664НСК 11628 2015-01-18 00:00:00.000 12 1 12 1
16 00664НСК 11628 2015-01-19 00:00:00.000 10 1 10 0
17 00664НСК 11628 2015-01-20 00:00:00.000 24 1 12 0
18 00664НСК 11628 2015-01-21 00:00:00.000 6 1 6 0
19 00664НСК 11628 2015-01-24 00:00:00.000 6 1 6 0
20 00664НСК 11628 2015-01-25 00:00:00.000 6 0 6 0
21 00664НСК 11628 2015-01-26 00:00:00.000 10 0 10 1
22 00664НСК 11628 2015-01-27 00:00:00.000 6 1 6 0
23 00664НСК 11628 2015-01-28 00:00:00.000 10 1 10 0
24 00664НСК 11628 2015-01-29 00:00:00.000 70 0 12 1
25 00664НСК 11628 2015-01-30 00:00:00.000 100 1 12 1
Similar question I have asked for R and got working solution, but now I want do it using T-SQL.
I need observe such a condition: if y = 1 and mark1 = 1, then the output by mark1=1 must be replaced by the first value that goes for y = 0 and mark1 = 1 in the output variable.
If the first value that goes for Y = 0 and mark1 = 1 in the output is more than the ship_count, then in output left the actual value of ship_count
Zero category of mark1 for output, we don't touch.
This operation must be done by group ship_code+product_id
So the desired output should look like this:
shop_code product_id doc_date ship_count mark_1 outputer y
----------------------------------------------------------------------------
1 00664НСК 11628 2015-01-03 00:00:00.000 12 1 *12 1
2 00664НСК 11628 2015-01-05 00:00:00.000 7 1 *7 1
3 00664НСК 11628 2015-01-06 00:00:00.000 24 0 24 1
4 00664НСК 11628 2015-01-07 00:00:00.000 18 1 *12 1
5 00664НСК 11628 2015-01-08 00:00:00.000 12 1 *12 1
6 00664НСК 11628 2015-01-09 00:00:00.000 18 0 18 1
7 00664НСК 11628 2015-01-10 00:00:00.000 6 0 6 1
8 00664НСК 11628 2015-01-11 00:00:00.000 6 1 6 1
9 00664НСК 11628 2015-01-12 00:00:00.000 6 1 6 1
10 00664НСК 11628 2015-01-13 00:00:00.000 18 1 *12 0
11 00664НСК 11628 2015-01-14 00:00:00.000 6 1 6 0
12 00664НСК 11628 2015-01-15 00:00:00.000 18 1 12 0
13 00664НСК 11628 2015-01-16 00:00:00.000 12 1 *10 1
14 00664НСК 11628 2015-01-17 00:00:00.000 18 1 *10 1
15 00664НСК 11628 2015-01-18 00:00:00.000 12 1 *10 1
16 00664НСК 11628 2015-01-19 00:00:00.000 10 1 10 0
17 00664НСК 11628 2015-01-20 00:00:00.000 24 1 12 0
18 00664НСК 11628 2015-01-21 00:00:00.000 6 1 6 0
19 00664НСК 11628 2015-01-24 00:00:00.000 6 1 6 0
20 00664НСК 11628 2015-01-25 00:00:00.000 6 0 6 0
21 00664НСК 11628 2015-01-26 00:00:00.000 10 0 10 1
22 00664НСК 11628 2015-01-27 00:00:00.000 6 1 6 1
23 00664НСК 11628 2015-01-28 00:00:00.000 20 1 *12 0
24 00664НСК 11628 2015-01-29 00:00:00.000 70 1 12 0
25 00664НСК 11628 2015-01-30 00:00:00.000 100 1 12 1

Good evening,
You should use a case statement to do your job.
For finding the first value for the describing clauses , use a subquery in which you keep the order that you wish(order by) and select the top 1 value.
Give a try and if you face some issues ask again.

Related

SQL replace incomplete days of the week in the month to 0

I have this table
date week_day
1-02-2018 4
2-02-2018 5
3-02-2018 6
4-02-2018 7
5-02-2018 1
6-02-2018 2
7-02-2018 3
................
26-02-2018 1
27-02-2018 2
28-02-2018 3
I need to get in SQL incomplete weeks to next in the following form:
date week_day
0 1
0 2
0 3
1-02-2018 4
2-02-2018 5
3-02-2018 6
4-02-2018 7
5-02-2018 1
6-02-2018 2
7-02-2018 3
................
26-02-2018 1
27-02-2018 2
28-02-2018 3
0 4
0 5
0 6
0 7
Is something like this what you're looking for:
DECLARE #FirstDate date =
(
SELECT MIN(DATEADD(DAY, (week_day - 1) * -1, [date]))
FROM YourTable
)
DECLARE #LastDate date =
(
SELECT MAX(DATEADD(DAY, 7 - week_day, [date]))
FROM YourTable
)
;
WITH cteDates
AS
(
SELECT
#FirstDate [date]
, 1 week_day
UNION ALL
SELECT
DATEADD(DAY, 1, [date])
, CASE WHEN week_day + 1 > 7 THEN 1 ELSE week_day + 1 END
FROM cteDates
WHERE [date] < #LastDate
)
SELECT
T.[date]
, C.week_day
FROM
cteDates C
LEFT JOIN YourTable T ON
C.[date] = T.[date]
AND C.week_day = T.week_day
This query seems to solve your query. I assume that your minimal date is over 1900-01-01. If not put any other date that is under than your minimal date and is monday
Also there may be better solution with recursive cte
with cte as (
select cast(a as datetime) date, b week_day from (values ('20180201',4)
,('20180202',5)
,('20180203',6)
,('20180204',7)
,('20180205',1)
,('20180206',2)
,('20180207',3)
,('20180226',1)
,('20180227',2)
,('20180228',3)
,('20181231',1)
,('20190101',2)
,('20190102',3)
,('20190103',4)
,('20190104',5)
) t(a,b)
)
select
iif((rn = 1 and charindex(k, st) > 0) or k is null, convert(varchar(10), date, 120), '0')
, isnull(k, week_day)
from (
select
date, week_day, ww
, concat(max(iif(week_day = 1, 1, null)) over (partition by ww)
, max(iif(week_day = 2, 2, null)) over (partition by ww)
, max(iif(week_day = 3, 3, null)) over (partition by ww)
, max(iif(week_day = 4, 4, null)) over (partition by ww)
, max(iif(week_day = 5, 5, null)) over (partition by ww)
, max(iif(week_day = 6, 6, null)) over (partition by ww)
, max(iif(week_day = 7, 7, null)) over (partition by ww)) st
, row_number() over (partition by ww order by week_day) rn
from
cte
cross apply (select datediff(dd, cast('19000101' as datetime), date) / 7 ww) q
) t
left join (values ('1'),('2'),('3'),('4'),('5'),('6'),('7')) z(k)
on
rn = 1
and (charindex(k, st) = 0 or iif(rn = 1, week_day, 0) = k)
The main key is to get the cartesian join of the (year, month, week) and weekdays then do a left join on the dates in question. The second key is to adjust for Monday being to the start of the week.
Here is an example that works in MySQL. It has one edge case which I have not yet resolved. The case of a month's last day ending on a Sunday. In that case, there are 7 zeros between the end of that month and the start of the next.
select * from (
select cartesian.`year`
, cartesian.`month`
, cartesian.`week`
, coalesce(dates.`date`,0) `date`, cartesian.weekday+1 week_day from (
/* get distinct cartesian results for the (year, month, week) and weekday */
select `year`
, `month`
, case weekday when 6 then `week` - 1 else `week` end `week`
, weekday
from (
select distinct year(`date`) `year`
, month(`date`) `month`
, week(`date`) `week`
, weeks.weekday
from date_48361641, (
select 0 weekday union
select 1 weekday union
select 2 weekday union
select 3 weekday union
select 4 weekday union
select 5 weekday union
select 6 weekday
) weeks
) weeks
-- order by `year`, `month`, `week`, `weekday`
) cartesian
left join (
/* adjust for edge case of week 0 */
select `date`
, `year`
, `month`
, `week`
, weekday
from (
/* adjust the week to allow for Monday start of week*/
select `date`
, `year`
, `month`
, case weekday when 6 then `week` - 1 else `week` end `week`
, weekday
from (
select `date`
, year(`date`) `year`
, month(`date`) `month`
, week(`date`) `week`
, weekday(`date`) weekday
from date_48361641 dates
-- order by `date`
) dates
) dates
) dates
on dates.`year` = cartesian.`year`
and dates.`month` = cartesian.`month`
and dates.`week` = cartesian.`week`
and dates.`weekday` = cartesian.`weekday`
) results
where results.`week` > -1
order by
results.`year`
, results.`month`
, results.`week`
, results.`week_day`
-- results
year month week date week_day
2016 1 0 0 1
2016 1 0 0 2
2016 1 0 0 3
2016 1 0 0 4
2016 1 0 2016-01-01 5
2016 1 0 2016-01-02 6
2016 1 0 2016-01-03 7
...
2016 1 4 2016-01-25 1
2016 1 4 2016-01-26 2
2016 1 4 2016-01-27 3
2016 1 4 2016-01-28 4
2016 1 4 2016-01-29 5
2016 1 4 2016-01-30 6
2016 1 4 2016-01-31 7
2016 1 5 0 1
2016 1 5 0 2
2016 1 5 0 3
2016 1 5 0 4
2016 1 5 0 5
2016 1 5 0 6
2016 2 4 0 7
2016 2 5 2016-02-01 1
2016 2 5 2016-02-02 2
2016 2 5 2016-02-03 3
2016 2 5 2016-02-04 4
2016 2 5 2016-02-05 5
2016 2 5 2016-02-06 6
2016 2 5 2016-02-07 7
...
2016 2 9 2016-02-29 1
2016 2 9 0 2
2016 2 9 0 3
2016 2 9 0 4
2016 2 9 0 5
2016 2 9 0 6
2016 3 8 0 7
2016 3 9 0 1
2016 3 9 2016-03-01 2
2016 3 9 2016-03-02 3
2016 3 9 2016-03-03 4
2016 3 9 2016-03-04 5
2016 3 9 2016-03-05 6
2016 3 9 2016-03-06 7
...
2016 3 13 2016-03-28 1
2016 3 13 2016-03-29 2
2016 3 13 2016-03-30 3
2016 3 13 2016-03-31 4
2016 3 13 0 5
2016 3 13 0 6
2016 4 12 0 7
2016 4 13 0 1
2016 4 13 0 2
2016 4 13 0 3
2016 4 13 0 4
2016 4 13 2016-04-01 5
2016 4 13 2016-04-02 6
2016 4 13 2016-04-03 7
...
2016 5 22 2016-05-30 1
2016 5 22 2016-05-31 2
2016 5 22 0 3
2016 5 22 0 4
2016 5 22 0 5
2016 5 22 0 6
2016 6 21 0 7
2016 6 22 0 1
2016 6 22 0 2
2016 6 22 2016-06-01 3
2016 6 22 2016-06-02 4
2016 6 22 2016-06-03 5
2016 6 22 2016-06-04 6
2016 6 22 2016-06-05 7
...
2016 6 26 2016-06-27 1
2016 6 26 2016-06-28 2
2016 6 26 2016-06-29 3
2016 6 26 2016-06-30 4
2016 6 26 0 5
2016 6 26 0 6
2016 7 25 0 7
2016 7 26 0 1
2016 7 26 0 2
2016 7 26 0 3
2016 7 26 0 4
2016 7 26 2016-07-01 5
2016 7 26 2016-07-02 6
2016 7 26 2016-07-03 7
...
2016 7 30 2016-07-25 1
2016 7 30 2016-07-26 2
2016 7 30 2016-07-27 3
2016 7 30 2016-07-28 4
2016 7 30 2016-07-29 5
2016 7 30 2016-07-30 6
2016 7 30 2016-07-31 7
2016 7 31 0 1
2016 7 31 0 2
2016 7 31 0 3
2016 7 31 0 4
2016 7 31 0 5
2016 7 31 0 6
2016 8 30 0 7
2016 8 31 2016-08-01 1
2016 8 31 2016-08-02 2
2016 8 31 2016-08-03 3
2016 8 31 2016-08-04 4
2016 8 31 2016-08-05 5
2016 8 31 2016-08-06 6
2016 8 31 2016-08-07 7
...
2016 8 35 2016-08-29 1
2016 8 35 2016-08-30 2
2016 8 35 2016-08-31 3
2016 8 35 0 4
2016 8 35 0 5
2016 8 35 0 6
2016 9 34 0 7
2016 9 35 0 1
2016 9 35 0 2
2016 9 35 0 3
2016 9 35 2016-09-01 4
2016 9 35 2016-09-02 5
2016 9 35 2016-09-03 6
2016 9 35 2016-09-04 7
...
2016 9 39 2016-09-26 1
2016 9 39 2016-09-27 2
2016 9 39 2016-09-28 3
2016 9 39 2016-09-29 4
2016 9 39 2016-09-30 5
2016 9 39 0 6
2016 10 38 0 7
2016 10 39 0 1
2016 10 39 0 2
2016 10 39 0 3
2016 10 39 0 4
2016 10 39 0 5
2016 10 39 2016-10-01 6
2016 10 39 2016-10-02 7
...
2016 10 44 2016-10-31 1
2016 10 44 0 2
2016 10 44 0 3
2016 10 44 0 4
2016 10 44 0 5
2016 10 44 0 6
2016 11 43 0 7
2016 11 44 0 1
2016 11 44 2016-11-01 2
2016 11 44 2016-11-02 3
2016 11 44 2016-11-03 4
2016 11 44 2016-11-04 5
2016 11 44 2016-11-05 6
2016 11 44 2016-11-06 7
...
2016 11 48 2016-11-28 1
2016 11 48 2016-11-29 2
2016 11 48 2016-11-30 3
2016 11 48 0 4
2016 11 48 0 5
2016 11 48 0 6
2016 12 47 0 7
2016 12 48 0 1
2016 12 48 0 2
2016 12 48 0 3
2016 12 48 2016-12-01 4
2016 12 48 2016-12-02 5
2016 12 48 2016-12-03 6
2016 12 48 2016-12-04 7
2016 12 49 2016-12-05 1
2016 12 49 2016-12-06 2
2016 12 49 2016-12-07 3
2016 12 49 2016-12-08 4
2016 12 49 2016-12-09 5
2016 12 49 2016-12-10 6
2016 12 49 2016-12-11 7
2016 12 50 2016-12-12 1
2016 12 50 2016-12-13 2
2016 12 50 2016-12-14 3
2016 12 50 2016-12-15 4
2016 12 50 2016-12-16 5
2016 12 50 2016-12-17 6
2016 12 50 2016-12-18 7
2016 12 51 2016-12-19 1
2016 12 51 2016-12-20 2
2016 12 51 2016-12-21 3
2016 12 51 2016-12-22 4
2016 12 51 2016-12-23 5
2016 12 51 2016-12-24 6
2016 12 51 2016-12-25 7
2016 12 52 2016-12-26 1
2016 12 52 2016-12-27 2
2016 12 52 2016-12-28 3
2016 12 52 2016-12-29 4
2016 12 52 2016-12-30 5
2016 12 52 2016-12-31 6
2017 1 0 2017-01-01 7
2017 1 1 2017-01-02 1
2017 1 1 2017-01-03 2
2017 1 1 2017-01-04 3
2017 1 1 2017-01-05 4
2017 1 1 2017-01-06 5
2017 1 1 2017-01-07 6
2017 1 1 2017-01-08 7
2017 1 2 2017-01-09 1
2017 1 2 2017-01-10 2
2017 1 2 2017-01-11 3
2017 1 2 2017-01-12 4
2017 1 2 2017-01-13 5
2017 1 2 2017-01-14 6
2017 1 2 2017-01-15 7
2017 1 3 2017-01-16 1
2017 1 3 2017-01-17 2
2017 1 3 2017-01-18 3
2017 1 3 2017-01-19 4
2017 1 3 2017-01-20 5
2017 1 3 2017-01-21 6
2017 1 3 2017-01-22 7
2017 1 4 2017-01-23 1
2017 1 4 2017-01-24 2
2017 1 4 2017-01-25 3
2017 1 4 2017-01-26 4
2017 1 4 2017-01-27 5
2017 1 4 2017-01-28 6
2017 1 4 2017-01-29 7
2017 1 5 2017-01-30 1
2017 1 5 2017-01-31 2
2017 1 5 0 3
2017 1 5 0 4
2017 1 5 0 5
2017 1 5 0 6
2017 2 4 0 7
2017 2 5 0 1
2017 2 5 0 2
2017 2 5 2017-02-01 3
2017 2 5 2017-02-02 4
2017 2 5 2017-02-03 5
2017 2 5 2017-02-04 6
2017 2 5 2017-02-05 7
2017 2 6 2017-02-06 1
2017 2 6 2017-02-07 2
2017 2 6 2017-02-08 3
2017 2 6 2017-02-09 4
2017 2 6 2017-02-10 5
2017 2 6 2017-02-11 6
2017 2 6 2017-02-12 7
2017 2 7 2017-02-13 1
2017 2 7 2017-02-14 2
2017 2 7 2017-02-15 3
2017 2 7 2017-02-16 4
2017 2 7 2017-02-17 5
2017 2 7 2017-02-18 6
2017 2 7 2017-02-19 7
2017 2 8 2017-02-20 1
2017 2 8 2017-02-21 2
2017 2 8 2017-02-22 3
2017 2 8 2017-02-23 4
2017 2 8 2017-02-24 5
2017 2 8 2017-02-25 6
2017 2 8 2017-02-26 7
2017 2 9 2017-02-27 1
2017 2 9 2017-02-28 2
2017 2 9 0 3
2017 2 9 0 4
2017 2 9 0 5
2017 2 9 0 6
2017 3 8 0 7
2017 3 9 0 1
2017 3 9 0 2
2017 3 9 2017-03-01 3
2017 3 9 2017-03-02 4
2017 3 9 2017-03-03 5
2017 3 9 2017-03-04 6
2017 3 9 2017-03-05 7
2017 3 10 2017-03-06 1
2017 3 10 2017-03-07 2
2017 3 10 2017-03-08 3
2017 3 10 2017-03-09 4
2017 3 10 2017-03-10 5
2017 3 10 2017-03-11 6
2017 3 10 2017-03-12 7
2017 3 11 2017-03-13 1
2017 3 11 2017-03-14 2
2017 3 11 2017-03-15 3
2017 3 11 2017-03-16 4
2017 3 11 2017-03-17 5
2017 3 11 2017-03-18 6
2017 3 11 2017-03-19 7
2017 3 12 2017-03-20 1
2017 3 12 2017-03-21 2
2017 3 12 2017-03-22 3
2017 3 12 2017-03-23 4
2017 3 12 2017-03-24 5
2017 3 12 2017-03-25 6
2017 3 12 2017-03-26 7
2017 3 13 2017-03-27 1
2017 3 13 2017-03-28 2
2017 3 13 2017-03-29 3
2017 3 13 2017-03-30 4
2017 3 13 2017-03-31 5
2017 3 13 0 6
2017 4 12 0 7
2017 4 13 0 1
2017 4 13 0 2
2017 4 13 0 3
2017 4 13 0 4
2017 4 13 0 5
2017 4 13 2017-04-01 6
2017 4 13 2017-04-02 7
2017 4 14 2017-04-03 1
2017 4 14 2017-04-04 2
2017 4 14 2017-04-05 3
2017 4 14 2017-04-06 4
2017 4 14 2017-04-07 5
2017 4 14 2017-04-08 6
2017 4 14 2017-04-09 7
2017 4 15 2017-04-10 1
2017 4 15 2017-04-11 2
2017 4 15 2017-04-12 3
2017 4 15 2017-04-13 4
2017 4 15 2017-04-14 5
2017 4 15 2017-04-15 6
2017 4 15 2017-04-16 7
2017 4 16 2017-04-17 1
2017 4 16 2017-04-18 2
2017 4 16 2017-04-19 3
2017 4 16 2017-04-20 4
2017 4 16 2017-04-21 5
2017 4 16 2017-04-22 6
2017 4 16 2017-04-23 7
2017 4 17 2017-04-24 1
2017 4 17 2017-04-25 2
2017 4 17 2017-04-26 3
2017 4 17 2017-04-27 4
2017 4 17 2017-04-28 5
2017 4 17 2017-04-29 6
2017 4 17 2017-04-30 7
2017 4 18 0 1
2017 4 18 0 2
2017 4 18 0 3
2017 4 18 0 4
2017 4 18 0 5
2017 4 18 0 6
2017 5 17 0 7
2017 5 18 2017-05-01 1
2017 5 18 2017-05-02 2
2017 5 18 2017-05-03 3
2017 5 18 2017-05-04 4
2017 5 18 2017-05-05 5
2017 5 18 2017-05-06 6
2017 5 18 2017-05-07 7
2017 5 19 2017-05-08 1
2017 5 19 2017-05-09 2
2017 5 19 2017-05-10 3
2017 5 19 2017-05-11 4
2017 5 19 2017-05-12 5
2017 5 19 2017-05-13 6
2017 5 19 2017-05-14 7
2017 5 20 2017-05-15 1
2017 5 20 2017-05-16 2
2017 5 20 2017-05-17 3
2017 5 20 2017-05-18 4
2017 5 20 2017-05-19 5
2017 5 20 2017-05-20 6
2017 5 20 2017-05-21 7
2017 5 21 2017-05-22 1
2017 5 21 2017-05-23 2
2017 5 21 2017-05-24 3
2017 5 21 2017-05-25 4
2017 5 21 2017-05-26 5
2017 5 21 2017-05-27 6
2017 5 21 2017-05-28 7
2017 5 22 2017-05-29 1
2017 5 22 2017-05-30 2
2017 5 22 2017-05-31 3
2017 5 22 0 4
2017 5 22 0 5
2017 5 22 0 6
2017 6 21 0 7
2017 6 22 0 1
2017 6 22 0 2
2017 6 22 0 3
2017 6 22 2017-06-01 4
2017 6 22 2017-06-02 5
2017 6 22 2017-06-03 6
2017 6 22 2017-06-04 7
2017 6 23 2017-06-05 1
2017 6 23 2017-06-06 2
2017 6 23 2017-06-07 3
2017 6 23 2017-06-08 4
2017 6 23 2017-06-09 5
2017 6 23 2017-06-10 6
2017 6 23 2017-06-11 7
2017 6 24 2017-06-12 1
2017 6 24 2017-06-13 2
2017 6 24 2017-06-14 3
2017 6 24 2017-06-15 4
2017 6 24 2017-06-16 5
2017 6 24 2017-06-17 6
2017 6 24 2017-06-18 7
2017 6 25 2017-06-19 1
2017 6 25 2017-06-20 2
2017 6 25 2017-06-21 3
2017 6 25 2017-06-22 4
2017 6 25 2017-06-23 5
2017 6 25 2017-06-24 6
2017 6 25 2017-06-25 7
2017 6 26 2017-06-26 1
2017 6 26 2017-06-27 2
2017 6 26 2017-06-28 3
2017 6 26 2017-06-29 4
2017 6 26 2017-06-30 5
2017 6 26 0 6
2017 7 25 0 7
2017 7 26 0 1
2017 7 26 0 2
2017 7 26 0 3
2017 7 26 0 4
2017 7 26 0 5
2017 7 26 2017-07-01 6
2017 7 26 2017-07-02 7
2017 7 27 2017-07-03 1
2017 7 27 2017-07-04 2
2017 7 27 2017-07-05 3
2017 7 27 2017-07-06 4
2017 7 27 2017-07-07 5
2017 7 27 2017-07-08 6
2017 7 27 2017-07-09 7
2017 7 28 2017-07-10 1
2017 7 28 2017-07-11 2
2017 7 28 2017-07-12 3
2017 7 28 2017-07-13 4
2017 7 28 2017-07-14 5
2017 7 28 2017-07-15 6
2017 7 28 2017-07-16 7
2017 7 29 2017-07-17 1
2017 7 29 2017-07-18 2
2017 7 29 2017-07-19 3
2017 7 29 2017-07-20 4
2017 7 29 2017-07-21 5
2017 7 29 2017-07-22 6
2017 7 29 2017-07-23 7
2017 7 30 2017-07-24 1
2017 7 30 2017-07-25 2
2017 7 30 2017-07-26 3
2017 7 30 2017-07-27 4
2017 7 30 2017-07-28 5
2017 7 30 2017-07-29 6
2017 7 30 2017-07-30 7
2017 7 31 2017-07-31 1
2017 7 31 0 2
2017 7 31 0 3
2017 7 31 0 4
2017 7 31 0 5
2017 7 31 0 6
2017 8 30 0 7
2017 8 31 0 1
2017 8 31 2017-08-01 2
2017 8 31 2017-08-02 3
2017 8 31 2017-08-03 4
2017 8 31 2017-08-04 5
2017 8 31 2017-08-05 6
2017 8 31 2017-08-06 7
2017 8 32 2017-08-07 1
2017 8 32 2017-08-08 2
2017 8 32 2017-08-09 3
2017 8 32 2017-08-10 4
2017 8 32 2017-08-11 5
2017 8 32 2017-08-12 6
2017 8 32 2017-08-13 7
2017 8 33 2017-08-14 1
2017 8 33 2017-08-15 2
2017 8 33 2017-08-16 3
2017 8 33 2017-08-17 4
2017 8 33 2017-08-18 5
2017 8 33 2017-08-19 6
2017 8 33 2017-08-20 7
2017 8 34 2017-08-21 1
2017 8 34 2017-08-22 2
2017 8 34 2017-08-23 3
2017 8 34 2017-08-24 4
2017 8 34 2017-08-25 5
2017 8 34 2017-08-26 6
2017 8 34 2017-08-27 7
2017 8 35 2017-08-28 1
2017 8 35 2017-08-29 2
2017 8 35 2017-08-30 3
2017 8 35 2017-08-31 4
2017 8 35 0 5
2017 8 35 0 6
2017 9 34 0 7
2017 9 35 0 1
2017 9 35 0 2
2017 9 35 0 3
2017 9 35 0 4
2017 9 35 2017-09-01 5
2017 9 35 2017-09-02 6
2017 9 35 2017-09-03 7
2017 9 36 2017-09-04 1
2017 9 36 2017-09-05 2
2017 9 36 2017-09-06 3
2017 9 36 2017-09-07 4
2017 9 36 2017-09-08 5
2017 9 36 2017-09-09 6
2017 9 36 2017-09-10 7
2017 9 37 2017-09-11 1
2017 9 37 2017-09-12 2
2017 9 37 2017-09-13 3
2017 9 37 2017-09-14 4
2017 9 37 2017-09-15 5
2017 9 37 2017-09-16 6
2017 9 37 2017-09-17 7
2017 9 38 2017-09-18 1
2017 9 38 2017-09-19 2
2017 9 38 2017-09-20 3
2017 9 38 2017-09-21 4
2017 9 38 2017-09-22 5
2017 9 38 2017-09-23 6
2017 9 38 2017-09-24 7
2017 9 39 2017-09-25 1
2017 9 39 2017-09-26 2
2017 9 39 2017-09-27 3
2017 9 39 2017-09-28 4
2017 9 39 2017-09-29 5
2017 9 39 2017-09-30 6
2017 10 39 2017-10-01 7
2017 10 40 2017-10-02 1
2017 10 40 2017-10-03 2
2017 10 40 2017-10-04 3
2017 10 40 2017-10-05 4
2017 10 40 2017-10-06 5
2017 10 40 2017-10-07 6
2017 10 40 2017-10-08 7
2017 10 41 2017-10-09 1
2017 10 41 2017-10-10 2
2017 10 41 2017-10-11 3
2017 10 41 2017-10-12 4
2017 10 41 2017-10-13 5
2017 10 41 2017-10-14 6
2017 10 41 2017-10-15 7
2017 10 42 2017-10-16 1
2017 10 42 2017-10-17 2
2017 10 42 2017-10-18 3
2017 10 42 2017-10-19 4
2017 10 42 2017-10-20 5
2017 10 42 2017-10-21 6
2017 10 42 2017-10-22 7
2017 10 43 2017-10-23 1
2017 10 43 2017-10-24 2
2017 10 43 2017-10-25 3
2017 10 43 2017-10-26 4
2017 10 43 2017-10-27 5
2017 10 43 2017-10-28 6
2017 10 43 2017-10-29 7
2017 10 44 2017-10-30 1
2017 10 44 2017-10-31 2
2017 10 44 0 3
2017 10 44 0 4
2017 10 44 0 5
2017 10 44 0 6
2017 11 43 0 7
2017 11 44 0 1
2017 11 44 0 2
2017 11 44 2017-11-01 3
2017 11 44 2017-11-02 4
2017 11 44 2017-11-03 5
2017 11 44 2017-11-04 6
2017 11 44 2017-11-05 7
2017 11 45 2017-11-06 1
2017 11 45 2017-11-07 2
2017 11 45 2017-11-08 3
2017 11 45 2017-11-09 4
2017 11 45 2017-11-10 5
2017 11 45 2017-11-11 6
2017 11 45 2017-11-12 7
2017 11 46 2017-11-13 1
2017 11 46 2017-11-14 2
2017 11 46 2017-11-15 3
2017 11 46 2017-11-16 4
2017 11 46 2017-11-17 5
2017 11 46 2017-11-18 6
2017 11 46 2017-11-19 7
2017 11 47 2017-11-20 1
2017 11 47 2017-11-21 2
2017 11 47 2017-11-22 3
2017 11 47 2017-11-23 4
2017 11 47 2017-11-24 5
2017 11 47 2017-11-25 6
2017 11 47 2017-11-26 7
2017 11 48 2017-11-27 1
2017 11 48 2017-11-28 2
2017 11 48 2017-11-29 3
2017 11 48 2017-11-30 4
2017 11 48 0 5
2017 11 48 0 6
2017 12 47 0 7
2017 12 48 0 1
2017 12 48 0 2
2017 12 48 0 3
2017 12 48 0 4
2017 12 48 2017-12-01 5
2017 12 48 2017-12-02 6
2017 12 48 2017-12-03 7
2017 12 49 2017-12-04 1
2017 12 49 2017-12-05 2
2017 12 49 2017-12-06 3
2017 12 49 2017-12-07 4
2017 12 49 2017-12-08 5
2017 12 49 2017-12-09 6
2017 12 49 2017-12-10 7
2017 12 50 2017-12-11 1
2017 12 50 2017-12-12 2
2017 12 50 2017-12-13 3
2017 12 50 2017-12-14 4
2017 12 50 2017-12-15 5
2017 12 50 2017-12-16 6
2017 12 50 2017-12-17 7
2017 12 51 2017-12-18 1
2017 12 51 2017-12-19 2
2017 12 51 2017-12-20 3
2017 12 51 2017-12-21 4
2017 12 51 2017-12-22 5
2017 12 51 2017-12-23 6
2017 12 51 2017-12-24 7
2017 12 52 2017-12-25 1
2017 12 52 2017-12-26 2
2017 12 52 2017-12-27 3
2017 12 52 2017-12-28 4
2017 12 52 2017-12-29 5
2017 12 52 2017-12-30 6
2017 12 52 2017-12-31 7
2017 12 53 0 1
2017 12 53 0 2
2017 12 53 0 3
2017 12 53 0 4
2017 12 53 0 5
2017 12 53 0 6
2018 1 0 2018-01-01 1
2018 1 0 2018-01-02 2
2018 1 0 2018-01-03 3
2018 1 0 2018-01-04 4
2018 1 0 2018-01-05 5
2018 1 0 2018-01-06 6
2018 1 0 2018-01-07 7
2018 1 1 2018-01-08 1
2018 1 1 2018-01-09 2
2018 1 1 2018-01-10 3
2018 1 1 2018-01-11 4
2018 1 1 2018-01-12 5
2018 1 1 2018-01-13 6
2018 1 1 2018-01-14 7
2018 1 2 2018-01-15 1
2018 1 2 2018-01-16 2
2018 1 2 2018-01-17 3
2018 1 2 2018-01-18 4
2018 1 2 2018-01-19 5
2018 1 2 2018-01-20 6
2018 1 2 2018-01-21 7
2018 1 3 2018-01-22 1
2018 1 3 2018-01-23 2
2018 1 3 2018-01-24 3
2018 1 3 2018-01-25 4
2018 1 3 2018-01-26 5
2018 1 3 2018-01-27 6
2018 1 3 2018-01-28 7
2018 1 4 2018-01-29 1
2018 1 4 2018-01-30 2
2018 1 4 2018-01-31 3
2018 1 4 0 4
2018 1 4 0 5
2018 1 4 0 6
2018 2 3 0 7
2018 2 4 0 1
2018 2 4 0 2
2018 2 4 0 3
2018 2 4 2018-02-01 4
2018 2 4 2018-02-02 5
2018 2 4 2018-02-03 6
2018 2 4 2018-02-04 7
2018 2 5 2018-02-05 1
2018 2 5 2018-02-06 2
2018 2 5 2018-02-07 3
2018 2 5 2018-02-08 4
2018 2 5 2018-02-09 5
2018 2 5 2018-02-10 6
2018 2 5 2018-02-11 7
2018 2 6 2018-02-12 1
2018 2 6 2018-02-13 2
2018 2 6 2018-02-14 3
2018 2 6 2018-02-15 4
2018 2 6 2018-02-16 5
2018 2 6 2018-02-17 6
2018 2 6 2018-02-18 7
2018 2 7 2018-02-19 1
2018 2 7 2018-02-20 2
2018 2 7 2018-02-21 3
2018 2 7 2018-02-22 4
2018 2 7 2018-02-23 5
2018 2 7 2018-02-24 6
2018 2 7 2018-02-25 7
2018 2 8 2018-02-26 1
2018 2 8 2018-02-27 2
2018 2 8 2018-02-28 3
2018 2 8 0 4
2018 2 8 0 5
2018 2 8 0 6
2018 3 7 0 7
2018 3 8 0 1
2018 3 8 0 2
2018 3 8 0 3
2018 3 8 2018-03-01 4
2018 3 8 2018-03-02 5
2018 3 8 2018-03-03 6
2018 3 8 2018-03-04 7
2018 3 9 2018-03-05 1
2018 3 9 2018-03-06 2
2018 3 9 2018-03-07 3
2018 3 9 2018-03-08 4
2018 3 9 2018-03-09 5
2018 3 9 2018-03-10 6
2018 3 9 2018-03-11 7
2018 3 10 2018-03-12 1
2018 3 10 2018-03-13 2
2018 3 10 2018-03-14 3
2018 3 10 2018-03-15 4
2018 3 10 2018-03-16 5
2018 3 10 2018-03-17 6
2018 3 10 2018-03-18 7
2018 3 11 2018-03-19 1
2018 3 11 2018-03-20 2
2018 3 11 2018-03-21 3
2018 3 11 2018-03-22 4
2018 3 11 2018-03-23 5
2018 3 11 2018-03-24 6
2018 3 11 2018-03-25 7
2018 3 12 2018-03-26 1
2018 3 12 2018-03-27 2
2018 3 12 2018-03-28 3
2018 3 12 2018-03-29 4
2018 3 12 2018-03-30 5
2018 3 12 2018-03-31 6
2018 4 12 2018-04-01 7
2018 4 13 2018-04-02 1
2018 4 13 2018-04-03 2
2018 4 13 2018-04-04 3
2018 4 13 2018-04-05 4
2018 4 13 2018-04-06 5
2018 4 13 2018-04-07 6
2018 4 13 2018-04-08 7
2018 4 14 2018-04-09 1
2018 4 14 2018-04-10 2
2018 4 14 2018-04-11 3
2018 4 14 2018-04-12 4
2018 4 14 2018-04-13 5
2018 4 14 2018-04-14 6
2018 4 14 2018-04-15 7
2018 4 15 2018-04-16 1
2018 4 15 2018-04-17 2
2018 4 15 2018-04-18 3
2018 4 15 2018-04-19 4
2018 4 15 2018-04-20 5
2018 4 15 2018-04-21 6
2018 4 15 2018-04-22 7
2018 4 16 2018-04-23 1
2018 4 16 2018-04-24 2
2018 4 16 2018-04-25 3
2018 4 16 2018-04-26 4
2018 4 16 2018-04-27 5
2018 4 16 2018-04-28 6
2018 4 16 2018-04-29 7
2018 4 17 2018-04-30 1
2018 4 17 0 2
2018 4 17 0 3
2018 4 17 0 4
2018 4 17 0 5
2018 4 17 0 6
2018 5 16 0 7
2018 5 17 0 1
2018 5 17 2018-05-01 2
2018 5 17 2018-05-02 3
2018 5 17 2018-05-03 4
2018 5 17 2018-05-04 5
2018 5 17 2018-05-05 6
2018 5 17 2018-05-06 7
2018 5 18 2018-05-07 1
2018 5 18 2018-05-08 2
2018 5 18 2018-05-09 3
2018 5 18 2018-05-10 4
2018 5 18 2018-05-11 5
2018 5 18 2018-05-12 6
2018 5 18 2018-05-13 7
2018 5 19 2018-05-14 1
2018 5 19 2018-05-15 2
2018 5 19 2018-05-16 3
2018 5 19 2018-05-17 4
2018 5 19 2018-05-18 5
2018 5 19 2018-05-19 6
2018 5 19 2018-05-20 7
2018 5 20 2018-05-21 1
2018 5 20 2018-05-22 2
2018 5 20 2018-05-23 3
2018 5 20 2018-05-24 4
2018 5 20 2018-05-25 5
2018 5 20 2018-05-26 6
2018 5 20 2018-05-27 7
2018 5 21 2018-05-28 1
2018 5 21 2018-05-29 2
2018 5 21 2018-05-30 3
2018 5 21 2018-05-31 4
2018 5 21 0 5
2018 5 21 0 6
2018 6 20 0 7
2018 6 21 0 1
2018 6 21 0 2
2018 6 21 0 3
2018 6 21 0 4
2018 6 21 2018-06-01 5
2018 6 21 2018-06-02 6
2018 6 21 2018-06-03 7
2018 6 22 2018-06-04 1
2018 6 22 2018-06-05 2
2018 6 22 2018-06-06 3
2018 6 22 2018-06-07 4
2018 6 22 2018-06-08 5
2018 6 22 2018-06-09 6
2018 6 22 2018-06-10 7
2018 6 23 2018-06-11 1
2018 6 23 2018-06-12 2
2018 6 23 2018-06-13 3
2018 6 23 2018-06-14 4
2018 6 23 2018-06-15 5
2018 6 23 2018-06-16 6
2018 6 23 2018-06-17 7
2018 6 24 2018-06-18 1
2018 6 24 2018-06-19 2
2018 6 24 2018-06-20 3
2018 6 24 2018-06-21 4
2018 6 24 2018-06-22 5
2018 6 24 2018-06-23 6
2018 6 24 2018-06-24 7
2018 6 25 2018-06-25 1
2018 6 25 2018-06-26 2
2018 6 25 2018-06-27 3
2018 6 25 2018-06-28 4
2018 6 25 2018-06-29 5
2018 6 25 2018-06-30 6
2018 7 25 2018-07-01 7
2018 7 26 2018-07-02 1
2018 7 26 2018-07-03 2
2018 7 26 2018-07-04 3
2018 7 26 2018-07-05 4
2018 7 26 2018-07-06 5
2018 7 26 2018-07-07 6
2018 7 26 2018-07-08 7
2018 7 27 2018-07-09 1
2018 7 27 2018-07-10 2
2018 7 27 2018-07-11 3
2018 7 27 2018-07-12 4
2018 7 27 2018-07-13 5
2018 7 27 2018-07-14 6
2018 7 27 2018-07-15 7
2018 7 28 2018-07-16 1
2018 7 28 2018-07-17 2
2018 7 28 2018-07-18 3
2018 7 28 2018-07-19 4
2018 7 28 2018-07-20 5
2018 7 28 2018-07-21 6
2018 7 28 2018-07-22 7
2018 7 29 2018-07-23 1
2018 7 29 2018-07-24 2
2018 7 29 2018-07-25 3
2018 7 29 2018-07-26 4
2018 7 29 2018-07-27 5
2018 7 29 2018-07-28 6
2018 7 29 2018-07-29 7
2018 7 30 2018-07-30 1
2018 7 30 2018-07-31 2
2018 7 30 0 3
2018 7 30 0 4
2018 7 30 0 5
2018 7 30 0 6
2018 8 29 0 7
2018 8 30 0 1
2018 8 30 0 2
2018 8 30 2018-08-01 3
2018 8 30 2018-08-02 4
2018 8 30 2018-08-03 5
2018 8 30 2018-08-04 6
2018 8 30 2018-08-05 7
2018 8 31 2018-08-06 1
2018 8 31 2018-08-07 2
2018 8 31 2018-08-08 3
2018 8 31 2018-08-09 4
2018 8 31 2018-08-10 5
2018 8 31 2018-08-11 6
2018 8 31 2018-08-12 7
2018 8 32 2018-08-13 1
2018 8 32 2018-08-14 2
2018 8 32 2018-08-15 3
2018 8 32 2018-08-16 4
2018 8 32 2018-08-17 5
2018 8 32 2018-08-18 6
2018 8 32 2018-08-19 7
2018 8 33 2018-08-20 1
2018 8 33 2018-08-21 2
2018 8 33 2018-08-22 3
2018 8 33 2018-08-23 4
2018 8 33 2018-08-24 5
2018 8 33 2018-08-25 6
2018 8 33 2018-08-26 7
2018 8 34 2018-08-27 1
2018 8 34 2018-08-28 2
2018 8 34 2018-08-29 3
2018 8 34 2018-08-30 4
2018 8 34 2018-08-31 5
2018 8 34 0 6
2018 9 33 0 7
2018 9 34 0 1
2018 9 34 0 2
2018 9 34 0 3
2018 9 34 0 4
2018 9 34 0 5
2018 9 34 2018-09-01 6
2018 9 34 2018-09-02 7
2018 9 35 2018-09-03 1
2018 9 35 2018-09-04 2
2018 9 35 2018-09-05 3
2018 9 35 2018-09-06 4
2018 9 35 2018-09-07 5
2018 9 35 2018-09-08 6
2018 9 35 2018-09-09 7
2018 9 36 2018-09-10 1
2018 9 36 2018-09-11 2
2018 9 36 2018-09-12 3
2018 9 36 2018-09-13 4
2018 9 36 2018-09-14 5
2018 9 36 2018-09-15 6
2018 9 36 2018-09-16 7
2018 9 37 2018-09-17 1
2018 9 37 2018-09-18 2
2018 9 37 2018-09-19 3
2018 9 37 2018-09-20 4
2018 9 37 2018-09-21 5
2018 9 37 2018-09-22 6
2018 9 37 2018-09-23 7
2018 9 38 2018-09-24 1
2018 9 38 2018-09-25 2
2018 9 38 2018-09-26 3
2018 9 38 2018-09-27 4
2018 9 38 2018-09-28 5
2018 9 38 2018-09-29 6
2018 9 38 2018-09-30 7
2018 9 39 0 1
2018 9 39 0 2
2018 9 39 0 3
2018 9 39 0 4
2018 9 39 0 5
2018 9 39 0 6
2018 10 38 0 7
2018 10 39 2018-10-01 1
2018 10 39 2018-10-02 2
2018 10 39 2018-10-03 3
2018 10 39 2018-10-04 4
2018 10 39 2018-10-05 5
2018 10 39 2018-10-06 6
2018 10 39 2018-10-07 7
2018 10 40 2018-10-08 1
2018 10 40 2018-10-09 2
2018 10 40 2018-10-10 3
2018 10 40 2018-10-11 4
2018 10 40 2018-10-12 5
2018 10 40 2018-10-13 6
2018 10 40 2018-10-14 7
2018 10 41 2018-10-15 1
2018 10 41 2018-10-16 2
2018 10 41 2018-10-17 3
2018 10 41 2018-10-18 4
2018 10 41 2018-10-19 5
2018 10 41 2018-10-20 6
2018 10 41 2018-10-21 7
2018 10 42 2018-10-22 1
2018 10 42 2018-10-23 2
2018 10 42 2018-10-24 3
2018 10 42 2018-10-25 4
2018 10 42 2018-10-26 5
2018 10 42 2018-10-27 6
2018 10 42 2018-10-28 7
2018 10 43 2018-10-29 1
2018 10 43 2018-10-30 2
2018 10 43 2018-10-31 3
2018 10 43 0 4
2018 10 43 0 5
2018 10 43 0 6
2018 11 42 0 7
2018 11 43 0 1
2018 11 43 0 2
2018 11 43 0 3
2018 11 43 2018-11-01 4
2018 11 43 2018-11-02 5
2018 11 43 2018-11-03 6
2018 11 43 2018-11-04 7
2018 11 44 2018-11-05 1
2018 11 44 2018-11-06 2
2018 11 44 2018-11-07 3
2018 11 44 2018-11-08 4
2018 11 44 2018-11-09 5
2018 11 44 2018-11-10 6
2018 11 44 2018-11-11 7
2018 11 45 2018-11-12 1
2018 11 45 2018-11-13 2
2018 11 45 2018-11-14 3
2018 11 45 2018-11-15 4
2018 11 45 2018-11-16 5
2018 11 45 2018-11-17 6
2018 11 45 2018-11-18 7
2018 11 46 2018-11-19 1
2018 11 46 2018-11-20 2
2018 11 46 2018-11-21 3
2018 11 46 2018-11-22 4
2018 11 46 2018-11-23 5
2018 11 46 2018-11-24 6
2018 11 46 2018-11-25 7
2018 11 47 2018-11-26 1
2018 11 47 2018-11-27 2
2018 11 47 2018-11-28 3
2018 11 47 2018-11-29 4
2018 11 47 2018-11-30 5
2018 11 47 0 6
2018 12 46 0 7
2018 12 47 0 1
2018 12 47 0 2
2018 12 47 0 3
2018 12 47 0 4
2018 12 47 0 5
2018 12 47 2018-12-01 6
2018 12 47 2018-12-02 7
2018 12 48 2018-12-03 1
2018 12 48 2018-12-04 2
2018 12 48 2018-12-05 3
2018 12 48 2018-12-06 4
2018 12 48 2018-12-07 5
2018 12 48 2018-12-08 6
2018 12 48 2018-12-09 7
2018 12 49 2018-12-10 1
2018 12 49 2018-12-11 2
2018 12 49 2018-12-12 3
2018 12 49 2018-12-13 4
2018 12 49 2018-12-14 5
2018 12 49 2018-12-15 6
2018 12 49 2018-12-16 7
2018 12 50 2018-12-17 1
2018 12 50 2018-12-18 2
2018 12 50 2018-12-19 3
2018 12 50 2018-12-20 4
2018 12 50 2018-12-21 5
2018 12 50 2018-12-22 6
2018 12 50 2018-12-23 7
2018 12 51 2018-12-24 1
2018 12 51 2018-12-25 2
2018 12 51 2018-12-26 3
2018 12 51 2018-12-27 4
2018 12 51 2018-12-28 5
2018 12 51 2018-12-29 6
2018 12 51 2018-12-30 7
2018 12 52 2018-12-31 1
2018 12 52 0 2
2018 12 52 0 3
2018 12 52 0 4
2018 12 52 0 5
2018 12 52 0 6
-- supporting SQL Schema
CREATE TABLE `date_48361641` (
`date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
truncate date_48361641 ;
insert into date_48361641 ( `date` )
select * from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date 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) v
where selected_date between '2016-01-01' and '2018-12-31' ;
Something like this should work
with allDates as (
see link below to finish this part
)
select DateFieldFromYourTable
, isnull(WeekDay, 0) DayOfWeek
from allDates left join YourTable on allDates.Something = DateFieldFromYourTable
Read this to get the code for your subquery.

Split DateTimeIndex data based on hour/minute/second

I have time-series data that I would like to split based on hour, or minute, or second. This is generally user-defined. I would like to know how it can be done.
For example, consider the following:
test = pd.DataFrame({'TIME': pd.date_range(start='2016-09-30',
freq='600s', periods=20)})
test['X'] = np.arange(20)
The output is:
TIME X
0 2016-09-30 00:00:00 0
1 2016-09-30 00:10:00 1
2 2016-09-30 00:20:00 2
3 2016-09-30 00:30:00 3
4 2016-09-30 00:40:00 4
5 2016-09-30 00:50:00 5
6 2016-09-30 01:00:00 6
7 2016-09-30 01:10:00 7
8 2016-09-30 01:20:00 8
9 2016-09-30 01:30:00 9
10 2016-09-30 01:40:00 10
11 2016-09-30 01:50:00 11
12 2016-09-30 02:00:00 12
13 2016-09-30 02:10:00 13
14 2016-09-30 02:20:00 14
15 2016-09-30 02:30:00 15
16 2016-09-30 02:40:00 16
17 2016-09-30 02:50:00 17
18 2016-09-30 03:00:00 18
19 2016-09-30 03:10:00 19
Suppose I want to split it by hour. I would like the following as one chunk which I can then save to a file.
TIME X
0 2016-09-30 00:00:00 0
1 2016-09-30 00:10:00 1
2 2016-09-30 00:20:00 2
3 2016-09-30 00:30:00 3
4 2016-09-30 00:40:00 4
5 2016-09-30 00:50:00 5
The second chunk would be:
TIME X
0 2016-09-30 01:00:00 6
1 2016-09-30 01:10:00 7
2 2016-09-30 01:20:00 8
3 2016-09-30 01:30:00 9
4 2016-09-30 01:40:00 10
5 2016-09-30 01:50:00 11
and so on...
Note I can do it purely based on logical conditions such as,
df[(df['TIME'] >= '2016-09-30 00:00:00') &
(df['TIME'] <= '2016-09-30 00:50:00')]
and repeat....
but what if my sampling changes? Is there a way to create a mask or something that takes less amount of code and is efficient? I have 10 GB of data.
Option 1
you can groupby series without having them in the object you're grouping.
test.groupby([test.TIME.dt.date,
test.TIME.dt.hour,
test.TIME.dt.minute,
test.TIME.dt.second]):
Option 2
use pd.TimeGrouper
test.set_index('TIME').groupby(pd.TimeGrouper('S')) # Group by seconds
test.set_index('TIME').groupby(pd.TimeGrouper('T')) # Group by minutes
test.set_index('TIME').groupby(pd.TimeGrouper('H')) # Group by hours
You need to use groupby for this, and the grouping should be based on date and hour:
test['DATE'] = test['TIME'].dt.date
test['HOUR'] = test['TIME'].dt.hour
grp = test.groupby(['DATE', 'HOUR'])
You can then loop over the groups and do the operation you want.
Example:
for key, df in grp:
print(key, df)
((datetime.date(2016, 9, 30), 0), TIME X DATE HOUR
0 2016-09-30 00:00:00 0 2016-09-30 0
1 2016-09-30 00:10:00 1 2016-09-30 0
2 2016-09-30 00:20:00 2 2016-09-30 0
3 2016-09-30 00:30:00 3 2016-09-30 0
4 2016-09-30 00:40:00 4 2016-09-30 0
5 2016-09-30 00:50:00 5 2016-09-30 0)
((datetime.date(2016, 9, 30), 1), TIME X DATE HOUR
6 2016-09-30 01:00:00 6 2016-09-30 1
7 2016-09-30 01:10:00 7 2016-09-30 1
8 2016-09-30 01:20:00 8 2016-09-30 1
9 2016-09-30 01:30:00 9 2016-09-30 1
10 2016-09-30 01:40:00 10 2016-09-30 1
11 2016-09-30 01:50:00 11 2016-09-30 1)
((datetime.date(2016, 9, 30), 2), TIME X DATE HOUR
12 2016-09-30 02:00:00 12 2016-09-30 2
13 2016-09-30 02:10:00 13 2016-09-30 2
14 2016-09-30 02:20:00 14 2016-09-30 2
15 2016-09-30 02:30:00 15 2016-09-30 2
16 2016-09-30 02:40:00 16 2016-09-30 2
17 2016-09-30 02:50:00 17 2016-09-30 2)
((datetime.date(2016, 9, 30), 3), TIME X DATE HOUR
18 2016-09-30 03:00:00 18 2016-09-30 3
19 2016-09-30 03:10:00 19 2016-09-30 3)

Dates between two dates from a table

I can't find the specific answer to this question but apologies if it has been asked previously.
I have the following example table which I have kept simple but it contains more rows and Types. It gets updated frequently.
Type From To Qty
1 2016-01-01 00:00:00.0000000 2016-01-03 00:00:00.0000000 30
1 2016-01-04 00:00:00.0000000 2016-01-05 00:00:00.0000000 31
1 2016-01-06 00:00:00.0000000 NULL 31
2 2016-04-24 00:00:00.0000000 NULL 15
I want to be able to update a table every day (as shown below) so it shows all of the dates between (and including) the From and To dates. The Qty for the relevant date must be displayed up to todays date where the TO is NULL.
Type Date Qty
1 2016-01-01 00:00:00.0000000 30
1 2016-01-02 00:00:00.0000000 30
1 2016-01-03 00:00:00.0000000 30
1 2016-04-04 00:00:00.0000000 31
1 2016-04-05 00:00:00.0000000 31
1 2016-04-06 00:00:00.0000000 31
1 2016-04-07 00:00:00.0000000 31
1 .... up to today where TO is NULL
1 2016-07-25 00:00:00.0000000 31
2 2016-04-24 00:00:00.0000000 15
2 .... up to today where TO is NULL
2 2016-07-25 00:00:00.0000000 15
Thank you in advance for your help.
Using Numbers table..
Demo Here
select b.*,qty from #test
cross apply
(
select dateadd(day,n,fromdate) from
numbers
where n<=
case when todate is null
then datediff(day,fromdate,getdate()) else datediff(day,fromdate,todate) end
) b(upd)
You can do this using a recursive CTE to generate all of the dates and JOIN to that for the result:
Test Data
Create Table Test
(
[Type] Int,
[From] Date,
[To] Date,
Qty Int
)
Insert Test
Values
(1, '2016-01-01', '2016-01-03', 30 ),
(1, '2016-01-04', '2016-01-05', 31 ),
(1, '2016-01-06', NULL, 31 ),
(2, '2016-04-24', NULL, 15 )
Query
;With MinMax As
(
Select Min([From]) MinFrom,
Max([To]) MaxTo,
Convert(Date, GetDate()) Today
From Test
), Date (Date) As
(
Select MinFrom
From MinMax
Union All
Select DateAdd(Day, 1, Date)
From Date
Where Date < (Select MaxTo From MinMax)
Or Date < (Select Today From MinMax)
)
Select T.[Type],
D.[Date],
T.Qty
From Test T
Join Date D On D.Date Between T.[From] And Coalesce(T.[To], Convert(Date, GetDate()))
Order By T.[Type], D.[Date]
Option (MaxRecursion 0)
Results
Type Date Qty
1 2016-01-01 30
1 2016-01-02 30
1 2016-01-03 30
1 2016-01-04 31
1 2016-01-05 31
1 2016-01-06 31
1 2016-01-07 31
1 2016-01-08 31
1 2016-01-09 31
1 2016-01-10 31
1 2016-01-11 31
1 2016-01-12 31
1 2016-01-13 31
1 2016-01-14 31
1 2016-01-15 31
1 2016-01-16 31
1 2016-01-17 31
1 2016-01-18 31
1 2016-01-19 31
1 2016-01-20 31
1 2016-01-21 31
1 2016-01-22 31
1 2016-01-23 31
1 2016-01-24 31
1 2016-01-25 31
1 2016-01-26 31
1 2016-01-27 31
1 2016-01-28 31
1 2016-01-29 31
1 2016-01-30 31
1 2016-01-31 31
1 2016-02-01 31
1 2016-02-02 31
1 2016-02-03 31
1 2016-02-04 31
1 2016-02-05 31
1 2016-02-06 31
1 2016-02-07 31
1 2016-02-08 31
1 2016-02-09 31
1 2016-02-10 31
1 2016-02-11 31
1 2016-02-12 31
1 2016-02-13 31
1 2016-02-14 31
1 2016-02-15 31
1 2016-02-16 31
1 2016-02-17 31
1 2016-02-18 31
1 2016-02-19 31
1 2016-02-20 31
1 2016-02-21 31
1 2016-02-22 31
1 2016-02-23 31
1 2016-02-24 31
1 2016-02-25 31
1 2016-02-26 31
1 2016-02-27 31
1 2016-02-28 31
1 2016-02-29 31
1 2016-03-01 31
1 2016-03-02 31
1 2016-03-03 31
1 2016-03-04 31
1 2016-03-05 31
1 2016-03-06 31
1 2016-03-07 31
1 2016-03-08 31
1 2016-03-09 31
1 2016-03-10 31
1 2016-03-11 31
1 2016-03-12 31
1 2016-03-13 31
1 2016-03-14 31
1 2016-03-15 31
1 2016-03-16 31
1 2016-03-17 31
1 2016-03-18 31
1 2016-03-19 31
1 2016-03-20 31
1 2016-03-21 31
1 2016-03-22 31
1 2016-03-23 31
1 2016-03-24 31
1 2016-03-25 31
1 2016-03-26 31
1 2016-03-27 31
1 2016-03-28 31
1 2016-03-29 31
1 2016-03-30 31
1 2016-03-31 31
1 2016-04-01 31
1 2016-04-02 31
1 2016-04-03 31
1 2016-04-04 31
1 2016-04-05 31
1 2016-04-06 31
1 2016-04-07 31
1 2016-04-08 31
1 2016-04-09 31
1 2016-04-10 31
1 2016-04-11 31
1 2016-04-12 31
1 2016-04-13 31
1 2016-04-14 31
1 2016-04-15 31
1 2016-04-16 31
1 2016-04-17 31
1 2016-04-18 31
1 2016-04-19 31
1 2016-04-20 31
1 2016-04-21 31
1 2016-04-22 31
1 2016-04-23 31
1 2016-04-24 31
1 2016-04-25 31
1 2016-04-26 31
1 2016-04-27 31
1 2016-04-28 31
1 2016-04-29 31
1 2016-04-30 31
1 2016-05-01 31
1 2016-05-02 31
1 2016-05-03 31
1 2016-05-04 31
1 2016-05-05 31
1 2016-05-06 31
1 2016-05-07 31
1 2016-05-08 31
1 2016-05-09 31
1 2016-05-10 31
1 2016-05-11 31
1 2016-05-12 31
1 2016-05-13 31
1 2016-05-14 31
1 2016-05-15 31
1 2016-05-16 31
1 2016-05-17 31
1 2016-05-18 31
1 2016-05-19 31
1 2016-05-20 31
1 2016-05-21 31
1 2016-05-22 31
1 2016-05-23 31
1 2016-05-24 31
1 2016-05-25 31
1 2016-05-26 31
1 2016-05-27 31
1 2016-05-28 31
1 2016-05-29 31
1 2016-05-30 31
1 2016-05-31 31
1 2016-06-01 31
1 2016-06-02 31
1 2016-06-03 31
1 2016-06-04 31
1 2016-06-05 31
1 2016-06-06 31
1 2016-06-07 31
1 2016-06-08 31
1 2016-06-09 31
1 2016-06-10 31
1 2016-06-11 31
1 2016-06-12 31
1 2016-06-13 31
1 2016-06-14 31
1 2016-06-15 31
1 2016-06-16 31
1 2016-06-17 31
1 2016-06-18 31
1 2016-06-19 31
1 2016-06-20 31
1 2016-06-21 31
1 2016-06-22 31
1 2016-06-23 31
1 2016-06-24 31
1 2016-06-25 31
1 2016-06-26 31
1 2016-06-27 31
1 2016-06-28 31
1 2016-06-29 31
1 2016-06-30 31
1 2016-07-01 31
1 2016-07-02 31
1 2016-07-03 31
1 2016-07-04 31
1 2016-07-05 31
1 2016-07-06 31
1 2016-07-07 31
1 2016-07-08 31
1 2016-07-09 31
1 2016-07-10 31
1 2016-07-11 31
1 2016-07-12 31
1 2016-07-13 31
1 2016-07-14 31
1 2016-07-15 31
1 2016-07-16 31
1 2016-07-17 31
1 2016-07-18 31
1 2016-07-19 31
1 2016-07-20 31
1 2016-07-21 31
1 2016-07-22 31
1 2016-07-23 31
1 2016-07-24 31
1 2016-07-25 31
1 2016-07-26 31
2 2016-04-24 15
2 2016-04-25 15
2 2016-04-26 15
2 2016-04-27 15
2 2016-04-28 15
2 2016-04-29 15
2 2016-04-30 15
2 2016-05-01 15
2 2016-05-02 15
2 2016-05-03 15
2 2016-05-04 15
2 2016-05-05 15
2 2016-05-06 15
2 2016-05-07 15
2 2016-05-08 15
2 2016-05-09 15
2 2016-05-10 15
2 2016-05-11 15
2 2016-05-12 15
2 2016-05-13 15
2 2016-05-14 15
2 2016-05-15 15
2 2016-05-16 15
2 2016-05-17 15
2 2016-05-18 15
2 2016-05-19 15
2 2016-05-20 15
2 2016-05-21 15
2 2016-05-22 15
2 2016-05-23 15
2 2016-05-24 15
2 2016-05-25 15
2 2016-05-26 15
2 2016-05-27 15
2 2016-05-28 15
2 2016-05-29 15
2 2016-05-30 15
2 2016-05-31 15
2 2016-06-01 15
2 2016-06-02 15
2 2016-06-03 15
2 2016-06-04 15
2 2016-06-05 15
2 2016-06-06 15
2 2016-06-07 15
2 2016-06-08 15
2 2016-06-09 15
2 2016-06-10 15
2 2016-06-11 15
2 2016-06-12 15
2 2016-06-13 15
2 2016-06-14 15
2 2016-06-15 15
2 2016-06-16 15
2 2016-06-17 15
2 2016-06-18 15
2 2016-06-19 15
2 2016-06-20 15
2 2016-06-21 15
2 2016-06-22 15
2 2016-06-23 15
2 2016-06-24 15
2 2016-06-25 15
2 2016-06-26 15
2 2016-06-27 15
2 2016-06-28 15
2 2016-06-29 15
2 2016-06-30 15
2 2016-07-01 15
2 2016-07-02 15
2 2016-07-03 15
2 2016-07-04 15
2 2016-07-05 15
2 2016-07-06 15
2 2016-07-07 15
2 2016-07-08 15
2 2016-07-09 15
2 2016-07-10 15
2 2016-07-11 15
2 2016-07-12 15
2 2016-07-13 15
2 2016-07-14 15
2 2016-07-15 15
2 2016-07-16 15
2 2016-07-17 15
2 2016-07-18 15
2 2016-07-19 15
2 2016-07-20 15
2 2016-07-21 15
2 2016-07-22 15
2 2016-07-23 15
2 2016-07-24 15
2 2016-07-25 15
2 2016-07-26 15

How to add status to the table

I have the following table where is clipping from my db. I have 2 types of contracts.
I: client pays for first 6mth 60$, next 6mth 120$ (111 client)
II: client pays for first 6mth 60$ but if want still pays 60$ the contract will be extended at 6mth, whole contract is 18mth. (321 client who still pays)
ID_Client | Amount | Amount_charge | Lenght | Date_from | Date_to | Reverse
--------------------------------------------------------------------------------
111 60 60 12 2015-01-01 2015-01-31 12
111 60 60 12 2015-02-01 2015-02-28 11
111 60 60 12 2015-03-01 2015-03-31 10
111 60 60 12 2015-04-01 2015-04-30 9
111 60 60 12 2015-05-01 2015-05-31 8
111 60 60 12 2015-06-01 2015-06-30 7
111 120 60 12 2015-07-01 2015-07-31 6
111 120 60 12 2015-08-01 2015-08-31 5
111 120 60 12 2015-09-01 2015-09-30 4
111 120 60 12 2015-10-01 2015-10-31 3
111 120 60 12 2015-11-01 2015-11-30 2
111 120 60 12 2015-12-01 2015-12-31 1
111 120 60 12 2016-01-01 2015-01-31 0
111 120 60 12 2016-02-01 2015-02-29 0
321 60 60 12 2015-01-01 2015-01-31 12
321 60 60 12 2015-02-01 2015-02-28 11
321 60 60 12 2015-03-01 2015-03-31 10
321 60 60 12 2015-04-01 2015-04-30 9
321 60 60 12 2015-05-01 2015-05-31 8
321 60 60 12 2015-06-01 2015-06-30 7
321 60 60 12 2015-07-01 2015-07-31 6
321 60 60 12 2015-08-01 2015-08-31 5
321 60 60 12 2015-09-01 2015-09-30 4
321 60 60 12 2015-10-01 2015-10-31 3
321 60 60 12 2015-11-01 2015-11-30 2
321 60 60 12 2015-12-01 2015-12-31 1
321 60 60 12 2016-01-01 2016-01-30 0
321 60 60 12 2016-02-01 2016-02-31 0
321 60 60 12 2016-03-01 2016-03-30 0
321 60 60 12 2016-04-01 2016-04-31 0
I need to add status column.
A - normal period of agreement
D - where the agreement is doubled after 6mth but after 12mth is E(nd of agreemnt)
E - where contract is finished
L - where contract after 6mth was extended, after 18mth the status will be type E
For 321 Client after 12mth the lenght of contract was updated from 12 to 18
I have a lot of clients so i think better will be using loop to go by all clients?
ID_Client | Amount | Amount_charge | Lenght | Date_from | Date_to | Reverse | Status
-----------------------------------------------------------------------------------------
111 60 60 12 2015-01-01 2015-01-31 12 A
111 60 60 12 2015-02-01 2015-02-28 11 A
111 60 60 12 2015-03-01 2015-03-31 10 A
111 60 60 12 2015-04-01 2015-04-30 9 A
111 60 60 12 2015-05-01 2015-05-31 8 A
111 60 60 12 2015-06-01 2015-06-30 7 A
111 120 60 12 2015-07-01 2015-07-31 6 D
111 120 60 12 2015-08-01 2015-08-31 5 D
111 120 60 12 2015-09-01 2015-09-30 4 D
111 120 60 12 2015-10-01 2015-10-31 3 D
111 120 60 12 2015-11-01 2015-11-30 2 D
111 120 60 12 2015-12-01 2015-12-31 1 D
111 120 60 12 2016-01-01 2015-01-31 0 E
111 120 60 12 2016-02-01 2015-02-29 0 E
321 60 60 12 2015-01-01 2015-01-31 12 A
321 60 60 12 2015-02-01 2015-02-28 11 A
321 60 60 12 2015-03-01 2015-03-31 10 A
321 60 60 12 2015-04-01 2015-04-30 9 A
321 60 60 12 2015-05-01 2015-05-31 8 A
321 60 60 12 2015-06-01 2015-06-30 7 A
321 60 60 12 2015-07-01 2015-07-31 6 L
321 60 60 12 2015-08-01 2015-08-31 5 L
321 60 60 12 2015-09-01 2015-09-30 4 L
321 60 60 12 2015-10-01 2015-10-31 3 L
321 60 60 12 2015-11-01 2015-11-30 2 L
321 60 60 12 2015-12-01 2015-12-31 1 L
321 60 60 18 2016-01-01 2016-01-30 0 L
321 60 60 18 2016-02-01 2016-02-31 0 L
321 60 60 18 2016-03-01 2016-03-30 0 L
321 60 60 18 2016-04-01 2016-04-31 0 L
If the Reverse column is what I think:
update table1 a
set "Status"=
CASE
WHEN A."Reverse" > 6 THEN
'A'
WHEN A."Reverse" > 0 THEN
DECODE (A."Amount", A."Amount_charge", 'L', 'D')
ELSE
CASE
WHEN A."Amount" <> A."Amount_charge" THEN
'E'
ELSE
CASE WHEN ADD_MONTHS ( (SELECT b."Date_from" FROM table1 b WHERE a."ID_Client" = b."ID_Client" AND b."Reverse" = 1),6) > a."Date_from" THEN 'L'
ELSE
'E'
END
END
END
Better is to calculate the sums. The amount per month come from first payment. Something like this:
DECLARE
CURSOR c2
IS
SELECT ID_CLIENT, --AMOUNT, AMOUNT_CHARGE, LENGTH, DATE_FROM, DATE_TO, REVERSE, STATUS,
FIRST_VALUE (amount_charge) OVER (PARTITION BY id_client ORDER BY date_from) first_amount_charge,
SUM (amount) OVER (PARTITION BY id_client ORDER BY date_from) sum_amount,
SUM (amount_charge) OVER (PARTITION BY id_client ORDER BY date_from) sum_amount_charge
FROM TABLE2
FOR UPDATE NOWAIT;
BEGIN
FOR c1 IN c2
LOOP
UPDATE table2
SET status = CASE WHEN c1.sum_amount <= 6 * c1.first_amount_charge THEN 'A'
WHEN c1.sum_amount > 18 * c1.first_amount_charge THEN 'E'
WHEN c1.sum_amount > c1.sum_amount_charge THEN 'D'
ELSE 'L'
END
WHERE CURRENT OF c2;
END LOOP;
END;

Pandas df row count

Date Ct
0 2015-04-01 1
1 2015-04-01 2
2 2015-04-01 3
3 2015-04-01 4
4 2015-04-02 1
5 2015-04-02 2
6 2015-04-02 3
7 2015-04-02 4
8 2015-04-03 1
9 2015-04-03 2
10 2015-04-03 3
11 2015-04-03 4
12 2015-04-04 1
13 2015-04-04 2
14 2015-04-04 3
15 2015-04-04 4
I have a string column 'Date' and I would like to create the 'Ct' column as represented below to maintain a count of the rows for a certain date. Date needs to be a string in my application, there will not always be an equal number of rows for each date, and 'Ct' will always count in the order of the numerical index. An answer or a nudge in the right direction would be greatly appreciated.
OK, this is a little weird but you can add a temp column and set this value to 1:
df['temp'] = 1
you can then perform a groupby on 'Date' and call transform on the 'temp' column to perform the count:
In [80]:
df['Ct'] = df.groupby('Date')['temp'].transform(pd.Series.cumsum)
df
Out[80]:
Date temp Ct
0 2015-04-01 1 1
1 2015-04-01 1 2
2 2015-04-01 1 3
3 2015-04-01 1 4
4 2015-04-02 1 1
5 2015-04-02 1 2
6 2015-04-02 1 3
7 2015-04-02 1 4
8 2015-04-03 1 1
9 2015-04-03 1 2
10 2015-04-03 1 3
11 2015-04-03 1 4
12 2015-04-04 1 1
13 2015-04-04 1 2
14 2015-04-04 1 3
15 2015-04-04 1 4
In [81]:
df.drop('temp',axis=1,inplace=True)
df
Out[81]:
Date Ct
0 2015-04-01 1
1 2015-04-01 2
2 2015-04-01 3
3 2015-04-01 4
4 2015-04-02 1
5 2015-04-02 2
6 2015-04-02 3
7 2015-04-02 4
8 2015-04-03 1
9 2015-04-03 2
10 2015-04-03 3
11 2015-04-03 4
12 2015-04-04 1
13 2015-04-04 2
14 2015-04-04 3
15 2015-04-04 4
The reason we can't just say perform the cumsum on the 'Date' column is because if it's a string then this will result in your date strings being concatenated with each other which is not what you want.
EDIT
Thanks to the master #Jeff for pointing out that the temp columns is unncecessary and you just use cumcount
In [150]:
df['Ct'] = df.groupby('Date').cumcount() + 1
df
Out[150]:
Date Ct
0 2015-04-01 1
1 2015-04-01 2
2 2015-04-01 3
3 2015-04-01 4
4 2015-04-02 1
5 2015-04-02 2
6 2015-04-02 3
7 2015-04-02 4
8 2015-04-03 1
9 2015-04-03 2
10 2015-04-03 3
11 2015-04-03 4
12 2015-04-04 1
13 2015-04-04 2
14 2015-04-04 3
15 2015-04-04 4