Skip to content
Advertisement

oracle grouping everytime the sum amount is below 15

I’ve following data:

and I need sum the amount, but for every sum <= 15000, they will be grouped together. If the amount is > 15000, they will be on their own group like this:

NOTE AMT group
1 1000 1
11 1000 1
5 1500 1
6 1600 1
2 2000 1
9 2100 1
4 3000 1
10 4500 2
3 8000 2
12 16000 3
7 20000 4
8 20000 5

I need the solution in oracle sql, is it possible? I’m using oracle 11g

Advertisement

Answer

One method is a recursive subquery:

Here is a db<>fiddle.

Note: This is ordering by amt — as in your sample data. You can as easily order by note (which also makes sense) just by adjusting seqnum in tt.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement