I am working in MySQL. Initially I had a table that looks like this:
My task is to calculate the percentage of bookings that were cancelled for people that were in a long waiting list and for people that had short waiting. After some manipulations I came up with the following code:
SELECT
CASE WHEN days_in_waiting_list > (SELECT AVG(days_in_waiting_list) FROM Bookings) THEN 'Long wait'
ELSE 'Short wait'
END AS waiting,
is_canceled, COUNT(*), count(*) * 100.0 / sum(count(*)) over() AS perc_cancelled
FROM Bookings
GROUP BY waiting, is_canceled;
The resulting table:
But I want the percentages to be calculated for the category, not the whole table. So that the sum of percentages in Short wait was equal to 100, the same is for Long wait. I want it to be like this:
| waiting | is_cancelled | perc |
|---|---|---|
| Short wait | 0 | 0.61 |
| Short wait | 1 | 0.39 |
| Long wait | 0 | 0.32 |
| Long wait | 1 | 0.68 |
Is there a way to do this? I know that it is possible using over(partition by waiting), but it gives me the error
Error Code: 1054. Unknown column 'waiting' in 'window partition by'

