Daily cancellation rate excluding banned users
MediumP3Coding
Tables:
users(id int primary key, banned boolean)
trips(id int primary key, client_id int, driver_id int, status text, request_at date)
status is 'completed', 'cancelled_by_driver' or 'cancelled_by_client'. client_id and driver_id refer to users.id.
For each day, return the cancellation rate: cancelled trips divided by all trips, rounded to 2 decimals. Only count trips where both the client and the driver are not banned. Days with no counted trips are not listed.
Output columns: day, cancellation_rate.
Schema
CREATE TABLE users ( id int PRIMARY KEY, banned boolean NOT NULL ); CREATE TABLE trips ( id int PRIMARY KEY, client_id int NOT NULL, driver_id int NOT NULL, status text NOT NULL, request_at date NOT NULL );
Example 1
- Input
- 2024-01-01: 4 trips between unbanned users, 2 of them cancelled
- Output
- (2024-01-01, 0.50)
Constraints
- PostgreSQL dialect.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
PostgreSQL
Run checks the 3 visible tests, Submit all 6. ⌘Enter runs, ⌘⇧Enter submits.