Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Select Distinct L.mid as mid,L.name as name, L.MFT_date as MFT_date, L.coupon as coupon, L.age as age, count(L.rid) as rid, sum(L.dur) as Dur , Round(Avg(L.dur),2) as Dur,
- Round(Sum(L.gross_rev),02) as Gross_rev, Round(Sum(L.net_rev),2) as net_rev, round(Avg(L.net_rev),2) as net_rev from
- (select A.mid as mid,A.name as name, A.rid as rid,(A.age) as age, A.dur as dur,IFNULL(E.charges,0) as gross_rev, B.coupon as coupon,
- 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, A.date - B.date as diff from
- (select r.id as rid, r.member_id as mid,m.display_name as name, Date(r.start_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
- where r.state = 'completed'
- and m.imaginary in ('sofam', 'normal')
- and r.member_id not in ('125', '127')
- group by 1) A
- left join
- ################## To find the first Booking
- (select distinct r.member_id as mid, min(r.id) as mrid, IFNULL(co.comment,"") as coupon,
- date(r.return_at + interval 8 hour) as date
- from reservations r left join members m on m.id = r.member_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) B
- on A.mid = B.mid
- ############## Total spent
- 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 L.diff < 30
- group by 1 ;
Advertisement
Add Comment
Please, Sign In to add comment