Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set @start := '2019-1-1';
- select B.mid,B.week,B.resv,SUM(E.gross),F.coupon_spent from
- (select
- distinct r.member_id as mid,
- weekofyear(r.start_at + interval '8' hour) as week,
- count(r.id) as resv
- from reservations r left join members m on m.id = r.member_id
- where r.state in ('completed','inUse')
- and Date(r.start_at + interval '8' hour) > @start
- and m.state = 'normal'
- and m.imaginary = 'normal'
- group by r.member_id, week) B
- left join
- (select c.member_id as mid, weekofyear(c.created_at + interval '8' hour) as week, sum(c.amount) as gross from charges c
- where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage') and c.created_at + interval '8' hour >= @start
- group by mid,week) E on B.mid = E.mid and B.week = E.week
- left join
- (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
- where p.state = 'normal' and p.paid_type = 'coupon'
- group by mid,week) F on F.mid = B.mid and B.week = F.week
- Group by B.week,B.mid
- order by mid asc
Advertisement
Add Comment
Please, Sign In to add comment