Oracle – Calculate Percent of Roads by Length Where Condition is >=15

aggregateoracleoracle-12cselect

I have a road condition table:

CONDITION       LENGTH
    2              5
    4             10
    6             15
    8             20
   10             25
   12             30
   14             35
   16             40
   18             45
   20             50

Roads that are at a condition of 15 or greater ( a.k.a. 15_OG) are considered to be in a good condition.

I want to calculate the percentage of roads (by length) that are at a good condition.

LENGTH_15_OG    TOTAL_LENGTH    PERCENT_15_OG
     135             275            49.09%

How can I do this calculation?

Best Answer

Just a little modification to save 1 table access:

select
  length_15_og, total_length,
  round(length_15_og/total_length*100, 2) as percent_15_og
from
(
  select
    sum(case when condition >= 15 then length end) as length_15_og,
    sum(length) as total_length
  from
    road_condition
);