The cancellation rate is computed by dividing the number of canceled
(by client or driver) requests with unbanned users by the total number
of requests with unbanned users on that day.Write a solution to find the cancellation rate of requests with
unbanned users (both client and driver must not be banned) each day
between "2013-10-01" and "2013-10-03". Round Cancellation Rate to two
decimal points.Return the result table in any order.
with ctes as
(
select *, count(*) as sums FROM
Trips join Users
on client_id = users_id
where banned = 'No'
group by request_at
)
select ctes.request_at as 'Day' ,round(count(ctes.id)/ctes.sums,2) as 'Cancellation Rate'
FROM
ctes
where ctes.status in ('cancelled_by_driver','cancelled_by_client')
group by ctes.request_at
Why I am getting Missing From Keyword error in the following code?
2
Answers
There are few issues in your code. Here is the code that works both with Oracle (fiddle) and MySql (fiddle).
Issues:
Sugestion – format your code consistently for better readability and maintainability.
NOTE:
I probably missed something cause there were no sample data nor expected result. You should adjust the code to your actual context. I didn’t bother with the math or aggregations. Where condition for status should probably be moved to ctes etc, etc… Just adjust it yourself.
There are mainly two errors in your query.
group by request_at
andselect *
. What would that even mean? "Give me one result row per request-id, but show me all values of every row regardless of the aggregation"?AS 'Day'
etc. are invalid. Single quotes denote string literals. If you must use quotes for names, use double quotes (AS "Day"
), but it is best to avoid them at all (AS day
).But it also seems you have the wrong approach altogether.
Where is this in your query? You are probably supposed to generate three rows, one for each date in the results.
Do this step by step: Get those requests for unbanned users. Then aggregate.
One thing to consider: There may be no trip on one of the three days. In that case you cannot calculate a rate, as the count is zero and you cannot divide by zero. Show NULL instead.
Something along the lines of:
In this code I assume, that request_at is a column containing mere dates. If it contains datetimes, then you must adjust this (e.g. by applying
TRUNC(request_at)
.