I'm seeking help with including deeper recurrence then last row with recursive query. Source data:
CREATE TABLE products_su(country, intprd, "period", su)AS VALUES
('GL', 'Medicine', '2019-04-01'::date, 57)
,('GL', 'Medicine', '2019-05-01', 298)
,('GL', 'Medicine', '2019-06-01', 860)
,('GL', 'Medicine', '2019-07-01', 1649)
,('GL', 'Medicine', '2019-08-01', 2227)
,('GL', 'Medicine', '2019-09-01', 1914)
,('GL', 'Medicine', '2019-10-01', 1751)
,('GL', 'Medicine', '2019-11-01', 2007)
,('GL', 'Medicine', '2019-12-01', 2649)
,('GL', 'Medicine', '2020-01-01', 2452)
,('GL', 'Medicine', '2020-02-01', 2733)
,('GL', 'Medicine', '2020-03-01', 3185)
,('GL', 'Medicine', '2020-04-01', 1768)
,('GL', 'Medicine', '2020-05-01', 1779)
,('GL', 'Medicine', '2020-06-01', 3030)
,('GL', 'Medicine', '2020-07-01', 3133)
,('GL', 'Medicine', '2020-08-01', 3373)
,('GL', 'Medicine', '2020-09-01', 4953)
,('GL', 'Medicine', '2020-10-01', 4478)
,('GL', 'Medicine', '2020-11-01', 4471)
,('GL', 'Medicine', '2020-12-01', 5212);
My query gives correct results for first 12 periods, but afterwards calculation for repeated patients changes twice: once, just for 13th period and then again for 14th period onwards. My calculation for repeated patients is accurate for firsts 12 months, but then calculation has got include previous 12th and 13th month, so query needs to modified twice and unioned twice:
1-12 month -- my calculation union 13 month -- new calculation recurring to value 12 periods ago union 14 month onwards -- new calculation recurring to value 12 and 13 periods ago
I am unable to put together values for new calculations.
Exact logic is in the spreadsheet below the query.
This is the query: It's Redshift but here's a PostgreSQL fiddle
with recursive build as (
select country,intprd,period,su,tpe,rp,rep_pat,cur_rn,max_rn
from(select country, intprd, period, su
, su / 6 as tpe
, 0::float as rp
, 0::float as rep_pat
, row_number()over(partition by country order by period) as rn
, 2::int as cur_rn
, count(1)over() as max_rn
from(select country, intprd, period, su::float
, row_number()over(partition by country order by period) as rn
from products_su)_)_
where rn = 1
union all
select t.country, t.intprd, t.period, t.su
, t.su/6 as tpe, b.tpe as rp
, least((b.tpe - b.rep_pat), t.tpe) as rep_pat
, b.cur_rn + 1 as cur_rn
, b.max_rn
from build b
join(select country, intprd, period, su::float, tpe
, 0::float as rep_pat
, lag(period)over(partition by country order by period) prev_period
, row_number()over(partition by country order by period) as rn
from(select country, intprd, period, SU
, SU/6 as tpe
, row_number()over(partition by country order by period) as rn
from products_su)_)as t
on t.prev_period = b.period
and t.country = b.country
and t.intprd = b.intprd
where t.rn = b.cur_rn
and b.cur_rn <= b.max_rn
)
select country, intprd, period, su
, round(tpe, 1) as tpe
, round(rep_pat, 1) as rep_pat
, round((tpe - rep_pat), 1) as new_pat
, round(sum(rep_pat+new_pat)over(partition by country order by period rows unbounded preceding), 1) as peq
from build
order by period;
country | intprd | period | su | tpe | rep_pat | new_pat | peq |
---|---|---|---|---|---|---|---|
GL | Medicine | 2019-04-01 | 57 | 9.5 | 0.0 | 9.5 | 9.5 |
GL | Medicine | 2019-05-01 | 298 | 49.7 | 9.5 | 40.2 | 59.2 |
GL | Medicine | 2019-06-01 | 860 | 143.3 | 40.2 | 103.2 | 202.6 |
GL | Medicine | 2019-07-01 | 1649 | 274.8 | 103.2 | 171.7 | 477.4 |
GL | Medicine | 2019-08-01 | 2227 | 371.2 | 171.7 | 199.5 | 848.6 |
GL | Medicine | 2019-09-01 | 1914 | 319.0 | 199.5 | 119.5 | 1167.6 |
GL | Medicine | 2019-10-01 | 1751 | 291.8 | 119.5 | 172.3 | 1459.4 |
GL | Medicine | 2019-11-01 | 2007 | 334.5 | 172.3 | 162.2 | 1793.9 |
GL | Medicine | 2019-12-01 | 2649 | 441.5 | 162.2 | 279.3 | 2235.4 |
GL | Medicine | 2020-01-01 | 2452 | 408.7 | 279.3 | 129.3 | 2644.0 |
GL | Medicine | 2020-02-01 | 2733 | 455.5 | 129.3 | 326.2 | 3099.6 |
GL | Medicine | 2020-03-01 | 3185 | 530.8 | 326.2 | 204.7 | 3630.4 |
GL | Medicine | 2020-04-01 13th period |
1768 | 294.7 | 204.7 expected 214.2 |
90.0 | 3925.1 |
GL | Medicine | 2020-05-01 14th period |
1779 | 296.5 | 90.0 expected 130.2 |
206.5 | 4221.6 |
GL | Medicine | 2020-06-01 | 3030 | 505.0 | 206.5 expected 309.7 |
298.5 | 4726.6 |
GL | Medicine | 2020-07-01 | 3133 | 522.2 | 298.5 expected 470.2 |
223.7 | 5248.8 |
GL | Medicine | 2020-08-01 | 3373 | 562.2 | 223.7 expected 423.2 |
338.5 | 5811.0 |
GL | Medicine | 2020-09-01 | 4953 | 825.5 | 338.5 expected 458.0 |
487.0 | 6636.5 |
GL | Medicine | 2020-10-01 | 4478 | 746.3 | 487.0 expected 659.3 |
259.3 | 7382.8 |
GL | Medicine | 2020-11-01 | 4471 | 745.2 | 259.3 expected 421.5 |
485.8 | 8127.9 |
GL | Medicine | 2020-12-01 | 5212 | 868.7 | 485.8 expected 765.2 |
382.8 | 8996.5 |
Below are the requirements and results to compare with excel formulas:
• Dosage: 6 tablets in month 1,2,13 and 14 of the treatment. No drug in 3rd and 4th year.
• # of Total Patient Equals who have taken the drug in a particular month are calculated by dividing standard units (SU) sold in that month by monthly dosage (considered as 6 tablets (SU))
• New patients taking drug in a particular month are calculated by subtracting the repeated patients number from the above number for that month. Repeated patients for a month are equivalent to new patients for the previous month.
• We assume 100% compliance of new patients in taking the second dosage and hence are accounted for next 12 months (next step)
• Final Total Patient Equals are calculated by summing up new patients number for recent 12 months i.e. the present month (for which the TPE are calculated) and the previous 11 months.
• Total Patient Equals share is calculated by dividing Total Patient Equals calculated using above methodology' by 'Sum of Total Patient Equals of all MS products'
• repeated Patients: repeated patients corrected to include patients from the previous year as well. After 12 months patients must be considered coming from the immediate previous month and from the month 12 months in the past who are starting again for the next year
• TOTAL_TPE's: summing done for previous 14 months instead of 12 to account for repeated patients form the month immendiately beofre the start for the time window (considered for the starting month) and the month 2 months removed from the start fo the window (considered for the ending month of the time window)
https://docs.google.com/spreadsheets/d/17j96xLV4jcx9YNj8qN2LzfH4MJHOS1iw-1TSb21LyNI/edit?gid=0#gid=0
Calculation changes from O20 into P20 and then Q20.
I will appreciate all the help :)