Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Select L.*, If(L.mrid = L.rid, 1,0), datediff(L.Resv_date1, L.MFT_date) as diff from
- (select A.mid as mid,A.name as name, A.rid as rid, B.mrid as mrid, A.coupon as coupon, A.car_name as car_name, (A.age) as age, A.dur as dur,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, B.date as MFT_date, A.date as Resv_date1 from
- (select r.id as rid, cc.car_name as car_name, IFNULL(co.comment,"") as coupon, r.member_id as mid,m.display_name as name, Date(r.return_at + interval 8 hour) as Date,
- round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,(YEAR(r.start_at) - YEAR(m.birthday)) as age
- from reservations r left join members m on m.id = r.member_id
- left join cars cr on cr.id = r.car_id
- left join car_classes cc on cc.id = cr.car_class_id
- left join coupons co on co.reservation_id = r.id
- where r.state = 'completed'
- and m.imaginary in ('sofam', 'normal')
- and r.member_id not in ('125', '127')
- group by 1) A
- left join
- (select distinct r.member_id as mid, min(r.id) as mrid, date(r.return_at + interval 8 hour) as date
- from reservations r left join members m on m.id = r.member_id
- where r.state = 'completed'
- and m.imaginary in ('sofam', 'normal')
- and r.member_id not in ('125', '127')
- group by 1) B
- on A.mid = B.mid
- 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')
- group by rid) E on A.rid = 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.rid = F.rid
- order by 1,2 asc) L
- where datediff(L.Resv_date1, L.MFT_date) < '57'
- group by 1,3 ;
Add Comment
Please, Sign In to add comment