Calculate not-nulls in group by

aggregategroup bynulloracle

I have a TRAILS table:

+----+------------+-----------------+
| ID |  LENGTH    | CONDITION (1-5) |
+----+------------+-----------------+
|  1 |    23.1    | null            |
|  2 |    41.7    | 1               |
|  3 |    10.9    | 4               |
|  4 |     6.2    | null            |
|  5 |    70.6    | 2               |
|  6 |    82.1    | 2               |
+----+------------+-----------------+

I want to summarize the table like this:

     +------------+-----------------+----------------------------+
     | TOT_LENGTH |  AVG_CONDITION  | CONDITION_PERCENT_COMPLETE |
     +------------+-----------------+----------------------------+
     |      234.6 |            2.25 |                        66% |
     +------------+-----------------+----------------------------+

I can do a GROUP BY to get the TOT_LENGTH and AVG_CONDITION, but I'
m not sure how to get the CONDITION_PERCENT_COMPLETE, without eliminating NULLS from the query/other columns.

How can I do this?

Best Answer

COUNT(condition) counts all rows in the group where condition is not null.

COUNT(*) counts all rows in the group.

COUNT(condition) / COUNT(*) * 100 returns the ratio of the above in percent.