I have looked into running total problems/questions asked here but could not find the similar one. Im using sql developer, and have select statement privileges, do not have access to cursors or loops, or creating functions.
I have a table with two columns requiring running total:
with my_table as
(
select 673 as customer, to_date('30.06.2021','dd.mm.yyyy') as report_date,210 as fee,210 as commission from dual union all
select 673 as customer, to_date('31.07.2021','dd.mm.yyyy') as report_date,210 as fee,0 as commission from dual union all
select 673 as customer, to_date('31.08.2021','dd.mm.yyyy') as report_date,210 as fee,210 as commission from dual union all
select 673 as customer, to_date('31.10.2021','dd.mm.yyyy') as report_date,210 as fee,310 as commission from dual union all
select 673 as customer, to_date('30.11.2021','dd.mm.yyyy') as report_date,210 as fee,210 as commission from dual union all
select 673 as customer, to_date('31.12.2021','dd.mm.yyyy') as report_date,210 as fee,0 as commission from dual union all
select 673 as customer, to_date('31.01.2022','dd.mm.yyyy') as report_date,210 as fee, 943.08 as commission from dual union all
select 673 as customer, to_date('28.02.2022','dd.mm.yyyy') as report_date,320 as fee,236.6 as commission from dual union all
select 673 as customer, to_date('31.03.2022','dd.mm.yyyy') as report_date,320 as fee,0 as commission from dual union all
select 673 as customer, to_date('30.04.2022','dd.mm.yyyy') as report_date,320 as fee,0 as commission from dual union all
select 673 as customer, to_date('31.05.2022','dd.mm.yyyy') as report_date,320 as fee,0 as commission from dual union all
select 673 as customer, to_date('30.06.2022','dd.mm.yyyy') as report_date,320 as fee,0 as commission from dual union all
select 673 as customer, to_date('31.07.2022','dd.mm.yyyy') as report_date,320 as fee,0 as commission from dual union all
select 673 as customer, to_date('31.08.2022','dd.mm.yyyy') as report_date,320 as fee,0 as commission from dual
)
I have to calculate running total for both fee and commission columns. For fee column, there is no rule or condition. The basic sum over with partition function is adequate. However when it comes to commission, I have to look out for running total of fee.
Every running_com value must be compared to the that of running_fee. If running_com exceed running_fee it should be replaced with running_fee on that row, and for the next row, cumulative total for commission should start with that value. here is the table and expected result:
| Customer | Report_date | Fee | Commission | Running_fee | Running_com |
|---|---|---|---|---|---|
| 673 | 30.06.2021 | 210 | 210 | 210 | 210 |
| 673 | 31.07.2021 | 210 | 0 | 420 | 210 |
| 673 | 31.08.2021 | 210 | 210 | 630 | 420 |
| 673 | 31.10.2021 | 210 | 310 | 840 | 730 |
| 673 | 30.11.2021 | 210 | 210 | 1050 | 940 |
| 673 | 31.12.2021 | 210 | 0 | 1260 | 940 |
| 673 | 31.01.2022 | 210 | 943.08 | 1470 | 1470 |
| 673 | 28.02.2022 | 320 | 236.6 | 1790 | 1706.6 |
| 673 | 31.03.2022 | 320 | 0 | 2110 | 1706.6 |
| 673 | 30.04.2022 | 320 | 0 | 2430 | 1706.6 |
| 673 | 31.05.2022 | 320 | 0 | 2750 | 1706.6 |
| 673 | 36.06.2022 | 320 | 0 | 3070 | 1706.6 |
| 673 | 31.07.2022 | 320 | 0 | 3390 | 1706.6 |
| 673 | 31.08.2022 | 320 | 0 | 3710 | 1706.6 |
I have put the previous commission value using lag, then try to sum commission value and previous commisssion but could not manage to, so to say looping part. it just summed up without running part.
Thanks for help.