-- 非禁止用户取消率=非禁止用户行程取消总数(包括司机取消的)/非禁止用户行程总数 方法一: select Request_at day,round(nvl(sum(casewhenStatus = 'completed'then0else1end),0)/count(1),2) "Cancellation Rate" from( select A.status, A.request_at from Trips A joinusers B on A.client_id = B.users_id where B.banned = 'No'and A.request_at between'2013-10-01'and'2013-10-03') groupby Request_at --方法二: select B.day,round(nvl(A.num,0)/B.num,2) "Cancellation Rate"from (select nvl(count(*),0) num,Request_at Dayfrom Trips A joinusers B on A.Client_Id=B.Users_Id --join users C on A.Client_Id=C.Users_Id where B.Banned='No'and A.Status=any('cancelled_by_driver','cancelled_by_client') and A.Request_at between'2013-10-01'and'2013-10-03' groupby Request_at) A rightjoin--使用外连接 防止丢失数据 (select nvl(count(*),1) num,Request_at Dayfrom Trips A joinusers B on A.Client_Id=B.Users_Id --join users C on A.Client_Id=C.Users_Id where B.Banned='No'and A.Request_at between'2013-10-01'and'2013-10-03' groupby Request_at) B on A.Day=B.Day orderby B.day