add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE example_data_expected (
SALES_DATE DATE,
CURR_DATE_UNITS_PURCHASED DECIMAL(38,10),
LTD_UNITS_PURCHASED DECIMAL(38,10),
CURR_DATE_EXTRAP_NUM_SOLD DECIMAL(38,10),
LTD_EXTRAP_NUM_SOLD DECIMAL(38,10)
);
INSERT INTO example_data_expected VALUES
('2023-11-01', 1000, 1000, 100, 100), -- 1,000 left
('2023-11-02', 0, 1000, 90, 190), -- 900 left (1,000 - 100)
('2023-11-03', 0, 1000, 81, 271), -- 810 left (1,000 - 190)
('2023-11-04', 200, 1200, 92.9, 363.9), -- 929 left (1,200 - 271)
('2023-11-05', 0, 1200, 83.6, 447.5), -- 863.1 left (1,200 - 363.9)
('2023-11-06', 0, 1200, 75.3, 522.8), -- 752.5 left (1,200 - 447.5)
('2023-11-07', 50, 1250, 72.7, 595.5), -- 727.2 left (1,250 - 522.8)
('2023-11-08', 0, 1250, 65.5, 661.0); -- 654.5 left (1,250 - 595.5)
INSERT INTO example_data_expected VALUES
('2023-11-09', 0, 1250, 58.906521, 719.841311)
,('2023-11-10', 0, 1250, 53.0158689, 772.8571799)
,('2023-11-11', 0, 1250, 47.71428201, 820.5714619)
;
11 rows affected
with params as(select cast(0.9 as decimal(38,10)) x)
,cte1 as(
select t.*
,row_number()over(order by sales_date) k
,sum(CURR_DATE_UNITS_PURCHASED)over(order by sales_date) roll_sum_purchased
,params.x
from example_data_expected t
cross apply params
)
,cte2 as(
select *
-- ,(sum(CURR_DATE_UNITS_PURCHASED*power(x,1-k))over(order by sales_date))*power(x,k) rest_sum
,roll_sum_purchased
-(sum(CURR_DATE_UNITS_PURCHASED*power(x,1-k))over(order by sales_date))*power(x,k) extrap_sum
from cte1
)
select SALES_DATE,k,roll_sum_purchased
,extrap_sum-lag(extrap_sum,1,0)over(order by SALES_DATE) curr_day_extrap
,CURR_DATE_EXTRAP_NUM_SOLD
,extrap_sum
,LTD_EXTRAP_NUM_SOLD
,LTD_EXTRAP_NUM_SOLD - extrap_sum check_extrap_sum
,CURR_DATE_UNITS_PURCHASED
from cte2
SALES_DATE k roll_sum_purchased curr_day_extrap CURR_DATE_EXTRAP_NUM_SOLD extrap_sum LTD_EXTRAP_NUM_SOLD check_extrap_sum CURR_DATE_UNITS_PURCHASED
2023-11-01 1 1000.0000000000 100.000000 100.0000000000 100.000000 100.0000000000 0.000000 1000.0000000000
2023-11-02 2 1000.0000000000 90.000000 90.0000000000 190.000000 190.0000000000 0.000000 0.0000000000
2023-11-03 3 1000.0000000000 81.000000 81.0000000000 271.000000 271.0000000000 0.000000 0.0000000000
2023-11-04 4 1200.0000000000 92.900000 92.9000000000 363.900000 363.9000000000 0.000000 200.0000000000
2023-11-05 5 1200.0000000000 83.610000 83.6000000000 447.510000 447.5000000000 -0.010000 0.0000000000
2023-11-06 6 1200.0000000000 75.249000 75.3000000000 522.759000 522.8000000000 0.041000 0.0000000000
2023-11-07 7 1250.0000000000 72.724100 72.7000000000 595.483100 595.5000000000 0.016900 50.0000000000
2023-11-08 8 1250.0000000000 65.451690 65.5000000000 660.934790 661.0000000000 0.065210 0.0000000000
2023-11-09 9 1250.0000000000 58.906521 58.9065210000 719.841311 719.8413110000 0.000000 0.0000000000
2023-11-10 10 1250.0000000000 53.015869 53.0158689000 772.857180 772.8571799000 0.000000 0.0000000000
2023-11-11 11 1250.0000000000 47.714282 47.7142820100 820.571462 820.5714619000 0.000000 0.0000000000
-- solution by Saikat
with base_cte as
(
select min(SALES_DATE) as minDate , max(SALES_DATE) as maxdate
from example_data_expected
) ,
recursive_cte as
(
select
SALES_DATE ,
CURR_DATE_UNITS_PURCHASED ,
LTD_UNITS_PURCHASED ,
cast(CURR_DATE_UNITS_PURCHASED as decimal(10,2)) as PREV_DAY_LTD_EXTRAP ,
cast(CURR_DATE_UNITS_PURCHASED*0.1 as decimal(10,1)) as
CURR_DATE_EXTRAP_NUM_SOLD
from example_data_expected where SALES_DATE = (select minDate from base_cte)
union all
select
b.SALES_DATE ,
b.CURR_DATE_UNITS_PURCHASED ,
b.LTD_UNITS_PURCHASED ,
cast(a.PREV_DAY_LTD_EXTRAP+b.CURR_DATE_UNITS_PURCHASED-a.CURR_DATE_EXTRAP_NUM_SOLD as decimal(10,2)) as PREV_DAY_LTD_EXTRAP ,
cast((a.PREV_DAY_LTD_EXTRAP+b.CURR_DATE_UNITS_PURCHASED-a.CURR_DATE_EXTRAP_NUM_SOLD)*0.1 as decimal(10,1)) as CURR_DATE_EXTRAP_NUM_SOLD
from recursive_cte as a inner join example_data_expected as b on dateadd(day,1,a.SALES_DATE) = b.SALES_DATE
where b.SALES_DATE <= (select maxdate from base_cte)
)
select SALES_DATE , CURR_DATE_UNITS_PURCHASED , LTD_UNITS_PURCHASED , CURR_DATE_EXTRAP_NUM_SOLD ,
sum(CURR_DATE_EXTRAP_NUM_SOLD) over(order by SALES_DATE) as LTD_EXTRAP_NUM_SOLD
from recursive_cte;
SALES_DATE CURR_DATE_UNITS_PURCHASED LTD_UNITS_PURCHASED CURR_DATE_EXTRAP_NUM_SOLD LTD_EXTRAP_NUM_SOLD
2023-11-01 1000.0000000000 1000.0000000000 100.0 100.0
2023-11-02 0.0000000000 1000.0000000000 90.0 190.0
2023-11-03 0.0000000000 1000.0000000000 81.0 271.0
2023-11-04 200.0000000000 1200.0000000000 92.9 363.9
2023-11-05 0.0000000000 1200.0000000000 83.6 447.5
2023-11-06 0.0000000000 1200.0000000000 75.3 522.8
2023-11-07 50.0000000000 1250.0000000000 72.7 595.5
2023-11-08 0.0000000000 1250.0000000000 65.5 661.0
2023-11-09 0.0000000000 1250.0000000000 58.9 719.9
2023-11-10 0.0000000000 1250.0000000000 53.0 772.9
2023-11-11 0.0000000000 1250.0000000000 47.7 820.6
with nums as(
select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
,(11),(12),(13),(14),(15),(16),(17),(18),(19),(20)
,(21),(22),(23),(24),(25),(26),(27),(28),(29),(30)
,(31),(32),(33),(34),(35),(36),(37),(38),(39),(40)
,(41),(42),(43),(44),(45),(46),(47),(48),(49),(50)
,(51),(52),(53),(54),(55),(56),(57),(58),(59),(60)
,(61),(62),(63),(64),(65),(66),(67),(68),(69),(70)
,(71),(72),(73),(74),(75),(76),(77),(78),(79),(80)
)t(n)
)
select *
,power(cast(0.9 as float),n) powern
,cast(1000.0 as float)-cast(1000.0 as float)*power(cast(0.9 as float),n) pn1000
,cast(1000.0 as float)*power(cast(0.9 as float),n) pn1000rest
from nums
n powern pn1000 pn1000rest
0 1 0 1000
1 0.9 100 900
2 0.81 190 810
3 0.729 271 729
4 0.6561 343.9 656.1
5 0.59049 409.51 590.49
6 0.531441 468.559 531.441
7 0.4782969 521.7031 478.2969
8 0.43046721 569.53279 430.46721
9 0.387420489 612.579511 387.420489
10 0.3486784401 651.3215599 348.6784401
11 0.31381059609 686.18940391 313.81059609
12 0.282429536481 717.570463519 282.429536481
13 0.2541865828329 745.8134171671 254.1865828329
14 0.22876792454961 771.23207545039 228.76792454961
15 0.205891132094649 794.108867905351 205.891132094649
16 0.185302018885184 814.697981114816 185.302018885184
17 0.166771816996666 833.228183003334 166.771816996666
18 0.150094635296999 849.905364703001 150.094635296999
19 0.135085171767299 864.914828232701 135.085171767299
20 0.121576654590569 878.423345409431 121.576654590569
21 0.109418989131512 890.581010868488 109.418989131512
22 0.0984770902183612 901.522909781639 98.4770902183612
23 0.0886293811965251 911.370618803475 88.6293811965251
24 0.0797664430768726 920.233556923127 79.7664430768726
25 0.0717897987691853 928.210201230815 71.7897987691853
26 0.0646108188922668 935.389181107733 64.6108188922668
27 0.0581497370030401 941.85026299696 58.1497370030401
28 0.0523347633027361 947.665236697264 52.3347633027361
29 0.0471012869724625 952.898713027538 47.1012869724625
30 0.0423911582752162 957.608841724784 42.3911582752162
31 0.0381520424476946 961.847957552305 38.1520424476946
32 0.0343368382029252 965.663161797075 34.3368382029252
33 0.0309031543826326 969.096845617367 30.9031543826326
34 0.0278128389443694 972.187161055631 27.8128389443694
35 0.0250315550499324 974.968444950068 25.0315550499324
36 0.0225283995449392 977.471600455061 22.5283995449392
37 0.0202755595904453 979.724440409555 20.2755595904453
38 0.0182480036314008 981.751996368599 18.2480036314007
39 0.0164232032682607 983.576796731739 16.4232032682607
40 0.0147808829414346 985.219117058565 14.7808829414346
41 0.0133027946472911 986.697205352709 13.3027946472911
42 0.011972515182562 988.027484817438 11.972515182562
43 0.0107752636643058 989.224736335694 10.7752636643058
44 0.00969773729787525 990.302262702125 9.69773729787525
45 0.00872796356808772 991.272036431912 8.72796356808772
46 0.00785516721127895 992.144832788721 7.85516721127895
47 0.00706965049015106 992.930349509849 7.06965049015106
48 0.00636268544113595 993.637314558864 6.36268544113595
49 0.00572641689702235 994.273583102978 5.72641689702235
50 0.00515377520732012 994.84622479268 5.15377520732012
51 0.00463839768658811 995.361602313412 4.63839768658811
52 0.0041745579179293 995.825442082071 4.1745579179293
53 0.00375710212613637 996.242897873864 3.75710212613637
54 0.00338139191352273 996.618608086477 3.38139191352273
55 0.00304325272217046 996.95674727783 3.04325272217046
56 0.00273892744995341 997.261072550047 2.73892744995341
57 0.00246503470495807 997.534965295042 2.46503470495807
58 0.00221853123446226 997.781468765538 2.21853123446226
59 0.00199667811101604 998.003321888984 1.99667811101604
60 0.00179701029991443 998.202989700086 1.79701029991443
61 0.00161730926992299 998.382690730077 1.61730926992299
62 0.00145557834293069 998.544421657069 1.45557834293069
63 0.00131002050863762 998.689979491362 1.31002050863762
64 0.00117901845777386 998.820981542226 1.17901845777386
65 0.00106111661199647 998.938883388004 1.06111661199647
66 0.000955004950796827 999.044995049203 0.955004950796827
67 0.000859504455717144 999.140495544283 0.859504455717144
68 0.00077355401014543 999.226445989855 0.77355401014543
69 0.000696198609130887 999.303801390869 0.696198609130887
70 0.000626578748217798 999.373421251782 0.626578748217798
71 0.000563920873396018 999.436079126604 0.563920873396018
72 0.000507528786056417 999.492471213944 0.507528786056416
73 0.000456775907450775 999.543224092549 0.456775907450775
74 0.000411098316705697 999.588901683294 0.411098316705697
75 0.000369988485035128 999.630011514965 0.369988485035128
76 0.000332989636531615 999.667010363468 0.332989636531615
77 0.000299690672878453 999.700309327122 0.299690672878453
78 0.000269721605590608 999.730278394409 0.269721605590608
79 0.000242749445031547 999.757250554968 0.242749445031547
80 0.000218474500528393 999.781525499472 0.218474500528393