Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set @startdate1 = '2019-03-01';
- set @startdate2 = '2019-12-30';
- use socar_malaysia;
- Select Month(I.return_date) as month, Count(Distinct I.mid) as Unique_Free_Riders_Per_month, Count(I.rid) as Unique_Free_reservations_per_month, I.net_rev_WO_Insurance as net_rev_WO_Insurance from
- (select A.return_date as return_date, A.rid2 as rid,A.mid as mid, IFNULL(E.charges,0) as gross_rev,
- IFNULL(F.coupon_Spent,0) as Coupon_Spent,
- round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev,round((IFNULL(E.charges,0) - IFNULL(G.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev_WO_Insurance from
- (select
- distinct r.id as rid2, r.member_id as mid,
- Date(r.return_at + interval '8' hour) as return_Date
- from reservations r left join members m on r.member_id = m.id
- where r.state in ('completed')
- and r.return_at + interval 8 hour >= @startdate1
- and r.return_at + interval 8 hour <= @startdate2
- and m.imaginary in ('sofam', 'normal')
- and r.member_id not in ('125', '127')
- group by rid2) A
- left join
- (select c.reservation_id as rid, sum(c.amount) as charges from charges c
- where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
- group by rid) E on A.rid2 = E.rid
- left join
- (select p.reservation_id as rid, month(p.created_at + interval '8' hour) as month,Year(p.created_at + interval '8' hour) as year, IFNULL(p.amount,0) as coupon_Spent from payments p
- where p.state = 'normal' and p.paid_type = 'coupon'
- group by p.reservation_id) F on A.rid2 = F.rid
- left join
- (select c.reservation_id as rid, sum(c.amount) as charges from charges c
- where c.state='normal' and c.kind in ('insurance')
- group by rid) G on A.rid2 = G.rid) I
- Where I.net_rev_WO_Insurance = 0
- group by 1
Advertisement
Add Comment
Please, Sign In to add comment