ahmedrahil786

member - reservations - second time - coupon value

Jul 19th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. set @start := '2019-1-1';
  2. select B.mid,B.week,B.resv,SUM(E.gross),F.coupon_spent from
  3. (select
  4. distinct r.member_id as mid,
  5. weekofyear(r.start_at + interval '8' hour) as week,
  6. count(r.id) as resv
  7. from reservations r left join members m on m.id = r.member_id
  8. where r.state in ('completed','inUse')
  9. and Date(r.start_at + interval '8' hour) > @start
  10. and m.state = 'normal'
  11. and m.imaginary = 'normal'
  12. group by r.member_id, week) B
  13. left join
  14. (select c.member_id as mid, weekofyear(c.created_at + interval '8' hour) as week, sum(c.amount) as gross from charges c
  15. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage') and c.created_at + interval '8' hour >= @start
  16. group by mid,week) E on B.mid = E.mid and B.week = E.week
  17. left join
  18. (select p.member_id as mid, weekofyear(p.created_at + interval '8' hour) as week, IFNULL(p.amount,0) as coupon_Spent from payments p
  19. where p.state = 'normal' and p.paid_type = 'coupon'
  20. group by mid,week) F on F.mid = B.mid and B.week = F.week
  21. Group by B.week,B.mid
  22. order by mid asc
Advertisement
Add Comment
Please, Sign In to add comment