oracle10g - Oracle cumulative totals by week for a given date range -
i using following query column totals week given date range...
select to_char(week_start - 1, 'dd-mon-yy') week_end, run_qty, acc_qty, case when run_qty <> 0 round(acc_qty/run_qty, 4) else 0 end pct (select week_start, sum(run_qty) run_qty, sum(acc_qty) acc_qty (select trunc(next_day(trunc(created_date), 'monday')) week_start, nvl(sum(run_qty), 0) run_qty, nvl(sum(accepted_qty), 0) acc_qty shema.table_a (some conditions) , created_date between :fromdate , :todate group trunc(next_day(trunc(created_date), 'monday')) union select trunc(next_day(trunc(to_date(zday, 'dd-mon-rrrr')), 'monday')) week_start, 0run_qty, 0acc_qty (select :fromdate + (level - 1) zday dual connect level <= (:todate - :fromdate))) group week_start order week_start desc)
with input paramters of :fromdate = 4/31/2015 , :todate = 6/25/2015, gives me last day of each week (week defined monday - sunday), run total each each week, accepted total each week, , pct of run total accepted each week, in result set looks so...
28-jun-2015 0 0 0
21-jun-2015 100 50 0.5
14-jun-2015 50 40 0.8
07-jun-2015 0 0 0
31-may-2015 0 0 0
24-may-2015 50 40 0.75
17-may-2015 80 50 0.625
10-may-2015 60 20 0.3333
03-may-2015 0 0 0
can use similar approach in order calculate running total of run , accepted quantities , percentage of quantity accepted on date range provided? (to give me result set like)...
28-jun-2015 340 200 0.5882
21-jun-2015 340 200 0.5882
14-jun-2015 240 150 0.625
07-jun-2015 190 110 0.5789
31-may-2015 190 110 0.5789
24-may-2015 190 110 0.5789
17-may-2015 140 70 0.5
10-may-2015 60 20 0.3333
03-may-2015 0 0 0
i figured out... in case runs thread searching solution similar issue, wrapped query above inside...
select week_end, run_qty, acc_qty, pct
from (query above)
model dimension by(row_number() on (order to_date(week_end, 'dd-mon-yy') asc) rec)
measures(week_end, run_qty, acc_qty, pct)
rules(
run_qty[rec > 1] order rec = run_qty[cv()] + run_qty[cv() - 1],
acc_qty[rec > 1] order rec = acc_qty[cv()] + acc_qty[cv() - 1],
pct[rec >= 0] order rec = case when run_qty[cv()] <> 0 round(acc_qty[cv()]/run_qty[cv()], 4) else 0 end )
order to_date(week_end, 'dd-mon-yy') desc
...and runs quickly, giving me expected results
Comments
Post a Comment