0

Using MySql 8.0, I have a table as below, and I'm trying to get the percentage of cancellations for each day.

| Request_at | Status              | Freq |
| ---------- | ------------------- | ---- |
| 2013-10-01 | completed           | 2    |
| 2013-10-01 | cancelled_by_client | 1    |
| 2013-10-02 | completed           | 2    |
| 2013-10-03 | completed           | 1    |
| 2013-10-03 | cancelled_by_driver | 1    |

How do I do so? The result should give:

| Date       | % cancel   |
| ---------- | -----------|
| 2013-10-01 |       0.33 |
| 2013-10-02 |       0.00 |
| 2013-10-03 |       0.50 |


0
SELECT Request_at AS `Date`, 
       SUM(CASE WHEN Status LIKE 'cancelled%'
                THEN Freq
                ELSE 0
                END) / SUM(Freq) AS `% cancel`
FROM source_table
GROUP BY Request_at 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.