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

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.

Related

Changing structure of a dataframe

I have five dataframes which have same indexes and columns, I mean all of them have same size.
These dataframes are Fixed_Cost, Variable_Cost, Semi_Variable_Cost, Marginal_Cost and Total_Cost. I want to change all dataframes in the same way.
This is Fixed_Cost:
date A B
2019-01-31 00:00:00 31,58 7,16
2019-02-28 00:00:00 17,11 12,30
2019-03-31 00:00:00 16,28 9,28
2019-04-30 00:00:00 23,63 18,31
2019-05-31 00:00:00 35,10 28,64
2019-06-30 00:00:00 34,50 20,34
2019-07-31 00:00:00 22,21 13,66
2019-08-31 00:00:00 19,91 7,15
2019-09-30 00:00:00 15,48 6,63
2019-10-31 00:00:00 18,06 10,19
2019-11-30 00:00:00 26,73 11,69
2019-12-31 00:00:00 36,69 11,15
2020-01-31 00:00:00 22,67 6,32
2020-02-29 00:00:00 24,12 10,72
2020-03-31 00:00:00 39,43 18,01
I want to change its structure to this:
Year Month Name Fixed_Cost
2019 1 A 31,58
2019 2 A 17,11
2019 3 A 16,28
2019 4 A 23,63
2019 5 A 35,10
2019 6 A 34,50
2019 7 A .
2019 8 A .
2019 9 A .
2019 10 A
2019 11 A
2019 12 A
2020 1 A
2020 2 A
2020 3 A
2019 1 B
2019 2 B
2019 3 B
2019 4 B
2019 5 B
2019 6 B
2019 7 B
2019 8 B
2019 9 B
2019 10 B
2019 11 B
2019 12 B
2020 1 B 6,32
2020 2 B 10,72
2020 3 B 18,01
Is it possible to make this change?
IIUC, assuming you want the data values filled out we can use assign and melt
#covert to datetime first.
#df['date'] = pd.to_datetime(df['date'])
df2 = (df.assign(year=(df['date'].dt.year)).assign(month=(df['date'].dt.month))
.drop('date',axis=1)
.melt(id_vars=['year','month'],var_name='name',value_name='fixed cost'))
print(df2)
year month name fixed cost
0 2019 1 A 31,58
1 2019 2 A 17,11
2 2019 3 A 16,28
3 2019 4 A 23,63
4 2019 5 A 35,10
5 2019 6 A 34,50
6 2019 7 A 22,21
7 2019 8 A 19,91
8 2019 9 A 15,48
9 2019 10 A 18,06
10 2019 11 A 26,73
11 2019 12 A 36,69
12 2020 1 A 22,67
13 2020 2 A 24,12
14 2020 3 A 39,43
15 2019 1 B 7,16
16 2019 2 B 12,30
17 2019 3 B 9,28
18 2019 4 B 18,31
19 2019 5 B 28,64
20 2019 6 B 20,34
21 2019 7 B 13,66
22 2019 8 B 7,15
23 2019 9 B 6,63
24 2019 10 B 10,19
25 2019 11 B 11,69
26 2019 12 B 11,15
27 2020 1 B 6,32
28 2020 2 B 10,72
29 2020 3 B 18,01
Approach for this is to use melt.
pd.melt(df, id_vars=['date'], var_name='description')
date description value
0 2019-01-31 A 3158
1 2019-02-28 A 233
2 2019-03-31 A 534
3 2019-01-31 B 12
4 2019-02-28 B 435
5 2019-03-31 B 64545
Then create new columns for Year and Month

Pandas : How can I assign group number according to specific value?

DataFrame
pd.DataFrame({'a': range(20)})
>>
a
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
Expected result:
a group_num
0 0 1
1 1 1
2 2 2
3 3 2
4 4 3
5 5 3
6 6 4
7 7 4
8 8 5
9 9 5
10 10 6
11 11 6
12 12 7
13 13 7
14 14 8
15 15 8
16 16 9
17 17 9
18 18 10
19 19 10
What I want to do is to assign group number, from 1 to 9, according to its value.
The idea is to sort these values and split them into 10 groups and assign from 1 to 9 to each group.
But have no idea how to implement it in Pandas
Need your helps
I believe need qcut for evenly sized bins:
df['b'] = pd.qcut(df['a'], 10, labels=range(1, 11))
print (df)
a b
0 0 1
1 1 1
2 2 2
3 3 2
4 4 3
5 5 3
6 6 4
7 7 4
8 8 5
9 9 5
10 10 6
11 11 6
12 12 7
13 13 7
14 14 8
15 15 8
16 16 9
17 17 9
18 18 10
19 19 10
And if you wanted to create groups of 2 you can use this:
df['b'] = df['a'].floordiv(2)+1
You can using //
df['G']=df.a//2+1
df
Out[609]:
a G
0 0 1
1 1 1
2 2 2
3 3 2
4 4 3
5 5 3
6 6 4
7 7 4
8 8 5
9 9 5
10 10 6
11 11 6
12 12 7
13 13 7
14 14 8
15 15 8
16 16 9
17 17 9
18 18 10
19 19 10

Update rank field based on most popular product

Trying to run an update on the following result set:
Row# ProductRankID ProductID ProductCategoryID ProductTypeID Rank Score
1 3 11266 9 80 0 765
2 14 25880 9 80 0 656
3 12 25864 9 80 0 547
4 7 11252 9 80 0 457
5 8 25719 9 80 0 456
6 4 13425 9 80 0 456
7 11 25677 9 80 0 456
8 9 25716 9 80 0 432
9 15 25714 9 80 0 324
10 13 13589 9 80 0 234
11 20 25803 9 80 0 234
12 17 25715 9 80 0 213
13 5 21269 9 80 0 154
14 10 25867 9 80 0 123
15 16 25676 9 80 0 123
16 22 17861 9 80 0 67
17 19 13534 9 80 0 55
18 23 13659 9 80 0 54
19 29 13658 9 80 0 34
20 21 13591 9 80 0 32
21 6 11249 9 80 0 23
22 18 11253 9 80 0 12
23 28 11253 9 87 0 65
24 27 13664 9 87 0 45
25 25 13658 9 87 0 14
26 26 13657 9 87 0 13
27 24 13659 9 87 0 13
28 30 11252 9 87 0 12
29 2 12345 11 80 0 324
I want the "Rank" column to be set 1...2..3..4 etc based on each row. Then on change of the ProductCategoryID + ProductTypeID, I want it to reset to 1...2...3...4 etc.
So the results should look something like:
Row# ProductRankID ProductID ProductCategoryID ProductTypeID Rank Score
1 3 11266 9 80 1 765
2 14 25880 9 80 2 656
3 12 25864 9 80 3 547
4 7 11252 9 80 4 457
5 8 25719 9 80 5 456
6 4 13425 9 80 6 456
7 11 25677 9 80 7 456
8 9 25716 9 80 8 432
9 15 25714 9 80 9 324
10 13 13589 9 80 10 234
11 20 25803 9 80 11 234
12 17 25715 9 80 12 213
13 5 21269 9 80 13 154
23 28 11253 9 87 1 65
24 27 13664 9 87 2 45
25 25 13658 9 87 3 14
26 26 13657 9 87 4 13
27 24 13659 9 87 5 13
28 30 11252 9 87 6 12
29 2 12345 11 80 1 324
Hope that makes some sense?
Thanks,
Richie
If you want a select:
select t.*,
row_number() over (partition by ProductCategoryID, ProductTypeID
order by score desc, productid
) as new_rank
from t;
If you want an update, use a CTE:
with toupdate as (
select t.*,
row_number() over (partition by ProductCategoryID, ProductTypeID
order by score desc, productid
) as new_rank
from t
)
update toupdate
set rank = new_rank;

pandas element wise conditional return index

I want to find abnormal values and replace them with corresponding day of next week.
year week day v1 v2
2001 1 1 46 9999
2001 1 2 60 9335
2001 1 3 9999 9318
2001 1 4 47 9999
2001 1 5 57 9373
2001 1 6 9999 9384
2001 1 7 72 9444
2001 2 1 75 73
2001 2 2 74 63
2001 2 3 79 377
2001 2 4 70 361
2001 2 5 75 73
2001 2 6 77 64
2001 2 7 76 57
I could carry out column by column,code as follows:
index_row=df[df['v1']==9999].index
for i in index_row:
df['v1'][i]=df['v1'][i+7] # i+7 is the index of next week
How to element-wise the whole dataframe? Such as pd.applymap.
How get the columns number(name) and row number base on conditional seiving values?
The target df I want as follows:
( * indicated modified values and the next week values)
year week day v1 v2
2001 1 1 46 *73
2001 1 2 60 9335
2001 1 3 *79 9318
2001 1 4 47 *361
2001 1 5 57 9373
2001 1 6 *77 9384
2001 1 7 72 9444
2001 2 1 75 *73
2001 2 2 74 63
2001 2 3 *79 377
2001 2 4 70 *361
2001 2 5 75 73
2001 2 6 *77 64
2001 2 7 76 57
create d1 with set_index on columns ['year', 'week', 'day']
create d2 with same index as d1 except, subtract 1 from week
mask with other
cols = ['year', 'week', 'day']
d1 = df.set_index(cols)
d2 = df.assign(week=df.week - 1).set_index(cols)
d1.mask(d1.eq(9999), d2).reset_index()
year week day v1 v2
0 2001 1 1 46 73
1 2001 1 2 60 9335
2 2001 1 3 79 9318
3 2001 1 4 47 361
4 2001 1 5 57 9373
5 2001 1 6 77 9384
6 2001 1 7 72 9444
7 2001 2 1 75 73
8 2001 2 2 74 63
9 2001 2 3 79 377
10 2001 2 4 70 361
11 2001 2 5 75 73
12 2001 2 6 77 64
13 2001 2 7 76 57
old answer
One approach is to setup d1 with index of ['year', 'week', 'day'] and manipulate that to shift a week. Then mask it for equal to 9999 and fillna
d1 = df.set_index(['year', 'week', 'day'])
s1 = d1.unstack(['year', 'day']).shift(-1).stack(['year', 'day']).swaplevel(0, 1)
d1.mask(d1==9999).fillna(s1).reset_index()
year week day v1 v2
0 2001 1 1 46.0 73.0
1 2001 1 2 60.0 9335.0
2 2001 1 3 79.0 9318.0
3 2001 1 4 47.0 361.0
4 2001 1 5 57.0 9373.0
5 2001 1 6 77.0 9384.0
6 2001 1 7 72.0 9444.0
7 2001 2 1 75.0 73.0
8 2001 2 2 74.0 63.0
9 2001 2 3 79.0 377.0
10 2001 2 4 70.0 361.0
11 2001 2 5 75.0 73.0
12 2001 2 6 77.0 64.0
13 2001 2 7 76.0 57.0
You can working with DatetimeIndex, set value by mask with shifted rows:
a = df['year'].astype(str).add('-').add(df['week'].astype(str))
.add('-').add(df['day'].sub(1).astype(str))
#http://strftime.org/
df.index = pd.to_datetime(a, format='%Y-%U-%w')
df2 = df.shift(-1,freq='7D')
df = df.mask(df.eq(9999), df2).reset_index(drop=True)
print (df)
year week day v1 v2
0 2001 1 1 46 73
1 2001 1 2 60 9335
2 2001 1 3 79 9318
3 2001 1 4 47 361
4 2001 1 5 57 9373
5 2001 1 6 77 9384
6 2001 1 7 72 9444
7 2001 2 1 75 73
8 2001 2 2 74 63
9 2001 2 3 79 377
10 2001 2 4 70 361
11 2001 2 5 75 73
12 2001 2 6 77 64
13 2001 2 7 76 57

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