Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set @start := '2019-6-1';
- select
- A.city,A.zid, A.name as zonename, A.region as region, A.Week as month, A.year as year, A.number_of_cars, A.cumulative_cars,
- IFNULL(D.nuse,0) as resv, IFNULL(B.ActiveMembers,0) as MAU, IFNULL(C.totalmfts,0) as MFTs,
- ROUND(IFNULL(B.ActiveMembers/A.number_of_cars,0),0) as Mau_percar,
- round(IFNULL(D.rmil/A.number_of_cars,0),2) as mil_vehicle
- from
- #### Number of cars
- (select
- month(czl.log_date + interval '8' hour) as week, year(czl.log_date + interval '8' hour) as year,
- count(distinct czl.id) as cumulative_cars,count(distinct czl.car_id) as number_of_cars,
- z.id as zid, z.name as name, z.city as city,z.region as region
- from car_zone_logs czl
- join cars ca on ca.id = czl.car_id
- join car_classes cc on cc.id = ca.car_class_id
- join zones z on z.id = czl.zone_id
- Where czl.log_date >= @start
- and czl.zone_id not in (2,3,101,383,378,712,818,786)
- group by week,zid) A
- left join
- ############## Number of Active Members
- (select
- month(r.return_at + interval '8' hour) as week,
- r.start_zone_id as zid,
- count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
- count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as resv
- from reservations r
- join members m
- on m.id = r.member_id and m.email not like '%socar.my%' and m.imaginary = 'normal'
- where r.return_at + interval '8' hour >= @start
- group by 1,2
- order by 1 asc) B
- on A.Week = B.Week and A.zid = B.zid
- left join
- ######### Number of MFTs
- (select
- A.week as week,
- IFNULL(B.zid,"") as zid,
- count(A.mid) as totalmfts
- from
- (select
- distinct c.member_id as mid, Month(c.created_at + interval '8' hour) as month, month(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,
- z.id as zid
- from reservations r
- join members m on r.member_id = m.id
- join zones z on r.start_zone_id = z.id
- where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
- group by r.member_id) B
- on A.mid = B.mid
- where B.zid is not null
- group by A.week,zid ) C
- on A.Week = C.Week and A.zid = C.zid
- left join
- ####### for Number of Hours
- (select
- month(r.return_at) as week, r.start_zone_id as zid, count(r.id) as nuse,sum(ra.mileage) as rmil,
- sum(timestampdiff(minute, r.start_at, r.return_at)/60) as dur,
- sum((timestampdiff(minute, r.start_at, r.end_at)/60)/24)*12 + if(mod((timestampdiff(minute, r.start_at, r.end_at)/60), 24)>=12,12,mod((timestampdiff(minute, r.start_at, r.end_at)/60), 24)) as adj_dur,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)) as rental,
- sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
- #count(distinct m.id) as mau
- from reservations r, members m , zones z , reservation_appendixes ra
- where r.member_id=m.id
- and r.start_zone_id = z.id
- and r.id = ra.reservation_id
- and r.way in ('round','d2d','oneway')
- and r.state = 'completed'
- and m.imaginary in ('normal', 'sofam')
- and r.return_at+interval 8 hour >= @start
- and r.start_zone_id not in (2,3,101,383,378,697,712,818)
- group by week, zid) D
- on D.week = A.week and D.zid = A.zid
- where A.week > month(now()) - 3
- group by A.week, A.zid
Advertisement
Add Comment
Please, Sign In to add comment