Skip to content
Advertisement

Oracle several counts over partition

I have the below query which is outputting the below results:

A B COUNT PERC
Correct Correct 968272 89.37
Mismatch Correct 52620 4.86
Correct NO VALUES 44630 4.12
Correct Mismatch 10722 0.99
NO VALUES NO VALUES 6217 0.57
Mismatch Mismatch 662 0.06
NO VALUES Mismatch 294 0.03

How could I get the below enhancing the query?

A B COUNT PERC COUNT_TOTAL_A PERC_PARTITION_BY_A
Correct Correct 968272 89.37 1023624 94.59
Mismatch Correct 52620 4.86 53282 98.76
Correct NO VALUES 44630 4.12 1023624 4.36
Correct Mismatch 10722 0.99 1023624 1.05
NO VALUES NO VALUES 6217 0.57 6511 95.48
Mismatch Mismatch 662 0.06 53282 1.24
NO VALUES Mismatch 294 0.03 6511 4.52

Advertisement

Answer

If you are in doubt how to combine the aggregation with the analytical function on different levels, simple split it in separate CTE

The first CTE calulates the overall count and percentage, the second the same for the A column.

Then join both results to get the complete report.

I’m not claimint that there is a simpler solution with one pass, but this solution works.

Interesting is also why you in your sample data expects e.g. for Correct three different values of PERC_PARTITION_BY_A

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