Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set @start := '2019-1-1';
- select
- A.mid,
- A.month,
- A.week,
- count(A.mid) as totalmfts,
- B.ridelength,
- B.carname,
- IFNULL(B.city,"") as City,
- IFNULL(B.region,"") as Region,
- IFNULL(E.charges,0) as gross_rev,
- IFNULL(F.coupon_Spent,0) as Coupon_Spent,
- round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev
- from
- (select
- distinct c.member_id as mid,
- Month(c.created_at + interval '8' hour) as month,
- WEEKOFYEAR(c.created_at + interval '8' hour) as Week
- from charges c
- where c.kind = 'subscriptionFee'
- and c.created_at + interval '8' hour >= @start) A
- left join
- (select
- distinct r.member_id as mid,
- min(r.id) as rid,
- timestampdiff(minute,CONVERT_TZ(r.start_at, '+00:00', '+8:00'), CONVERT_TZ(r.end_at, '+00:00', '+8:00'))/60 as ridelength,
- z.city as city,
- z.region as region,
- cc.car_name as carname
- from reservations r
- join members m on r.member_id = m.id
- join zones z on r.start_zone_id = z.id
- join cars ca on ca.id = r.car_id
- join car_classes cc on cc.id = ca.car_class_id
- where m.imaginary in ('sofam', 'normal')
- and r.member_id not in ('125', '127')
- and r.start_at + interval '8' hour >= @start
- and r.state = 'completed'
- group by r.member_id) B
- on A.mid = B.mid
- 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') and c.created_at + interval '8' hour >= @start
- group by rid) E on B.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 F.rid = E.rid
- where B.city is not null
- group by A.mid
Advertisement
Add Comment
Please, Sign In to add comment