ahmedrahil786

Rahil - Member- Revenue Generated per month

Jun 13th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. select
  2. r.member_id as memberid,
  3. m.display_name as name,
  4. month(r.start_at + interval '8' hour) as month,
  5. Year(r.start_at + interval '8' hour) as Year,
  6. count(r.id) as reservations,
  7. IFNULL(round(B.charges,2),0) as gross_rev,
  8. round((IFNULL(B.charges,0) - IFNULL(A.coupon,0)),2) as net_rev,
  9. round(IFNULL((IFNULL(A.coupon,0)/B.charges),0),2) as coupon_rate
  10. from reservations r
  11. join members m on m.id = r.member_id
  12. left join
  13. (select
  14. p.member_id as mid, month(p.created_at + interval '8' hour) as month,Year(p.created_at + interval '8' hour) as year, IFNULL(p.amount,0) as coupon
  15. from payments p
  16. where p.state = 'normal' and p.paid_type = 'coupon'
  17. group by p.member_id,month) A
  18. on A.mid = r.member_id and A.month = month(r.start_at + interval '8' hour) and A.year = Year(r.start_at + interval '8' hour)
  19. left join
  20. (select
  21. c.member_id as mid, month(c.created_at + interval '8' hour) as month, Year(c.created_at + interval '8' hour) as year, sum(c.amount) as charges
  22. from charges c
  23. where c.state = 'normal' and c.kind IN ('rent' , 'oneway', 'd2d', 'mileage')
  24. group by c.member_id,month) B
  25. on B.mid = r.member_id and B.month = month(r.start_at + interval '8' hour) and B.year = Year(r.start_at + interval '8' hour)
  26.  
  27. where m.imaginary in ('normal')
  28. and r.member_id not in ('125', '127')
  29. and r.state in ('completed','inUse')
  30. group by memberid,month
Advertisement
Add Comment
Please, Sign In to add comment