想法就是去创建临时表而不是去做连接,来提高数据查询速度,因为当一个查询涉及很多表时,查询的笛卡尔积是非常大的,单纯的用索引可能解决不了查询时间的问题.这个时候可以用临时表,将一个查询分成多步完成,减少输出查询结果时的笛卡尔积,会对提高查询效率有很大的帮助(这句话是教材里面的)
先要筛选出满足条件的临时表,再去计算取消率,这道题的想法是套用了双重临时表
select
Request_at Day,
round(count(case status when 'completed' then null else status end) / count(status),2) as 'Cancellation Rate'
from
(select
`status`, request_at
from trips
where
client_id not in (select users_id from users where banned = 'Yes')
and
driver_id not in (select users_id from users where banned = 'Yes')
)
where Request_at >= '2013-10-01'
and Request_at <= '2013-10-03'
group by request_at;