Skip to content
Advertisement

Oracle SQL UNION alternative

I’m using ORACLE SQL (11g), lets assume we have a table named TRANSMISSIONS which has field that holds file sizes

I want to perform various aggregate functions on various partitions let’s say for different file sizes. However I want the partitions to be cumulative. So a 10 KB file will be both in the <=500000 bytes partition and the <=2000000000 partition. So if I had 5 files <=500000 and 5 files > 50000 && < 2000000000 Then I would have the following reults:

So basically the initial approach was to do unions like this:

However if I have several of these partitions which I do it will result into a huge unmaintanable query where the only thing that changes basically is the label and the where clause.

Is there any better way to do this?

Advertisement

Answer

You are looking for cumulative sums. I think I would go for:

This gives you full flexibility on the ranges that you want — I am starting at 0, assuming that is the minimum possible value but the code could be adjusted if this is not the case.

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