SQL> create table trip(id int,client_id int,driver_id int,city_id int,status varchar2(30),request_at date);
Table created
SQL>
SQL> insert into trip values(1, 1 , 10 ,1,trim(' completed'), date '2013-10-01');
1 row inserted
SQL> insert into trip values(2, 2 , 11 ,1,trim(' cancelled_by_driver'), date '2013-10-01');
1 row inserted
SQL> insert into trip values(3, 3 , 12 ,6,trim(' completed'), date '2013-10-01');
1 row inserted
SQL> insert into trip values(4, 4 , 13 ,6,trim(' cancelled_by_client'), date '2013-10-01');
1 row inserted
SQL> insert into trip values(5, 1 , 10 ,1,trim(' completed'), date '2013-10-02');
1 row inserted
SQL> insert into trip values(6, 2 , 11 ,6,trim(' completed'), date '2013-10-02');
1 row inserted
SQL> insert into trip values(7, 3 , 12 ,6,trim(' completed'), date '2013-10-02');
1 row inserted
SQL> insert into trip values(8, 2 , 12 ,12 ,trim(' completed'), date '2013-10-03');
1 row inserted
SQL> insert into trip values(9, 3 , 10 ,12 ,trim(' completed'), date '2013-10-03');
1 row inserted
SQL> insert into trip values(10 , 4 , 13 ,12 ,trim(' cancelled_by_driver'), date '2013-10-03');
1 row inserted
SQL> commit;
Commit complete
SQL> create table usr(user_id int,banned varchar2(3),role varchar2(10));
Table created
SQL>
SQL> insert into usr values (1 ,'No' ,'client');
1 row inserted
SQL> insert into usr values (2 ,'Yes','client');
1 row inserted
SQL> insert into usr values (3 ,'No' ,'client');
1 row inserted
SQL> insert into usr values (4 ,'No' ,'client');
1 row inserted
SQL> insert into usr values (10,'No' ,'driver');
1 row inserted
SQL> insert into usr values (11,'No' ,'driver');
1 row inserted
SQL> insert into usr values (12,'No' ,'driver');
1 row inserted
SQL> insert into usr values (13,'No' ,'driver');
1 row inserted
SQL> commit;
Commit complete
SQL>
SQL> select trunc(t.request_at) as "Day",
2 sum(case when status'completed' then 1 else 0 end)/count(*) "Cancellation Rate"
3from trip t
4 where t.request_at between to_date('20131001','yyyymmdd') and to_date('20131003','yyyymmdd')
5 and t.client_id in (select user_id from usr where banned='No' )
6 and t.driver_id in (select user_id from usr where banned='No' )
7group by trunc(t.request_at)
8order by 1;
Day Cancellation Rate
----------- -----------------
2013/10/1 0.333333333333333
2013/10/2
0
2013/10/3
0.5
SQL>
|