ahmedrahil786

Members Life time spent for <15 RM - Optimize Rahil

Dec 2nd, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1.  
  2. Select Distinct L.mid as mid,L.name as name, L.MFT_date as MFT_date, count(L.rid) as rid,
  3. Round(Sum(L.gross_rev),02) as Gross_rev, Round(sum(L.net_rev),2) as net_rev, round(sum(L.net_rev),2) as net_rev, round(sum(L.net_rev_WO_Insurance),2) as net_rev from
  4. (select A.mid as mid,A.name as name, A.rid as rid,IFNULL(E.charges,0) as gross_rev,
  5. 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,
  6. round((IFNULL(E.charges,0) - IFNULL(G.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev_WO_Insurance
  7. from
  8. (select r.id as rid, r.member_id as mid,m.display_name as name, Date(r.start_at + interval 8 hour) as Date,
  9. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur
  10. from reservations r left join members m on m.id = r.member_id
  11. where r.state = 'completed'
  12. and m.imaginary in ('sofam', 'normal')
  13. and r.member_id not in ('125', '127')
  14. group by 1) A
  15. left join
  16. (select distinct r.member_id as mid, min(r.id) as mrid, date(r.return_at + interval 8 hour) as date
  17. from reservations r left join members m on m.id = r.member_id
  18. where r.state = 'completed'
  19. and m.imaginary in ('sofam', 'normal')
  20. and r.member_id not in ('125', '127')
  21. group by 1) B
  22. on A.mid = B.mid
  23. left join
  24. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  25. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  26. group by rid) E on A.rid = E.rid
  27. left join
  28. (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
  29. where p.state = 'normal' and p.paid_type = 'coupon'
  30. group by p.reservation_id) F on A.rid = F.rid
  31. left join
  32. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  33. where c.state='normal' and c.kind in ('insurance')
  34. group by rid) G on A.rid = G.rid
  35. order by 1,2 asc) L
  36. group by 1 ;
Advertisement
Add Comment
Please, Sign In to add comment