I have created this query to solve this problem,
SELECT c.ra as Day, round(count(cancelled) / Count(*),2) as "Cancellation Rate" FROM
(
select a.Id, a.ra, a.Status as status, b.Status as cancelled FROM
(
select Id, Client_Id, Driver_Id, Status, Request_at as ra from Trips t
where not exists
(
select 1 from users u
where (t.Client_Id = u.Users_Id or t.Driver_Id = u.Users_Id)
and u.banned = 'Yes'
)
) a LEFT JOIN
(
select Id, Client_Id, Driver_Id, Status, Request_at as ra from Trips t
where not exists
(
select 1 from users u
where (t.Client_Id = u.Users_Id or t.Driver_Id = u.Users_Id)
and u.banned = 'Yes'
) and (t.Status = 'cancelled_by_driver' or t.status = 'cancelled_by_client')
) b on a.Id = b.Id
) c GROUP BY c.ra
To make that query more concise, I changed it into the following, but that doesn't work. So, my question is, what is the right way of making such queries more concise?
SELECT c.ra as Day, round(count(cancelled) / Count(*),2) as "Cancellation Rate" FROM
(
select a.Id, a.ra, a.Status as status, b.Status as cancelled FROM
(
select Id, Client_Id, Driver_Id, Status, Request_at as ra from (
select * from Trips t
where not exists
(
select 1 from users u
where (t.Client_Id = u.Users_Id or t.Driver_Id = u.Users_Id)
and u.banned = 'Yes'
)
) t1
) a LEFT JOIN
(
select Id, Client_Id, Driver_Id, Status, Request_at as ra from t1
WHERE (Status = 'cancelled_by_driver' or Status = 'cancelled_by_client')
) b on a.Id = b.Id
) c GROUP BY c.ra