Elliott Davidson
Elliott Davidson

Reputation: 13

Oracle SQL percentages in a single query

So far I've got:

SELECT bike_no, maint_fault_details, maint_action_taken, 
       maint_fault_date, maint_action_date
FROM mainthistory
WHERE (maint_fault_date + 5) < maint_action_date ;

Trying to get it so that it displays the results as a percentage, ie, 10% of bikes took longer than 5 days to repair. Any idea's/suggestion?

Upvotes: 1

Views: 493

Answers (1)

Peter Lang
Peter Lang

Reputation: 55524

This will count all entries for which the condition is true and divide it by the number of rows.

SELECT 100 * SUM( CASE WHEN (maint_fault_date + 5) < maint_action_date
                   THEN 1
                   ELSE 0 END
       ) / COUNT(*) AS percentage
FROM mainthistory;

Upvotes: 2

Related Questions