Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######## weekly Revenue Related Info
- set @startDate = "2019-12-01 00:00:00";
- select
- rm.week as week,
- sum(rm.nr) as netrevenues,
- sum(rm.rent) as rental,
- sum(rm.d2d) as d2d,
- sum(O.oneway_amount) as oneway,
- sum(C.subscriptionFee) as subscriptionFee,
- sum(rm.mileage) as mileage,
- sum(A.Amount) as penalty_Repair,
- sum(rm.paid_coupon) as Coupon,
- IFNULL(B.noa,0) as Accidents,
- IFNULL(B.accidentamount,0) as accidentamount,
- IFNULL(sum(I.insurance),0) as insurance,
- rm.year as year
- from
- (select
- weekofyear(r.return_at + interval '8' hour) as week,
- year(r.return_at + interval '8' hour) as year,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent')),0)) as rent,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
- sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
- round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance')),0)- ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
- from reservations r, members m
- where r.member_id=m.id
- and m.imaginary in ('normal','sofam')
- and r.state = 'completed'
- and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
- group by 1,2
- ) rm
- left join
- ######### Weekly other amounts
- (select
- weekofyear(c.created_at + interval '8' hour) as week,
- year(c.created_at + interval '8' hour) as year,
- sum(c.amount) as amount
- from charges c
- where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
- and c.state = 'normal'
- and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee','insurance')
- group by 1,2) A on A.week = rm.week and A.year = rm.year
- left join
- (select
- weekofyear(c.created_at + interval 8 hour) as week,
- year(c.created_at + interval 8 hour) as year,
- sum(c.amount) as oneway_amount
- from charges c
- where c.kind in ('oneway')
- and c.state = 'normal'
- and (c.created_at + interval 8 hour) >= @startdate
- group by 1,2) O on O.week = rm.week and O.year = rm.year
- left join
- (select
- A.week as week,
- A.year as year,
- A.noa as noa,
- round(Sum(A.amount),0) as accidentamount
- from
- (
- select
- weekofyear(c.created_at + interval '8' hour) as week,
- year(c.created_at + interval '8' hour) as year,
- c.id as cid,
- count(distinct case when c.kind = 'accident' then c.id end) as noa,
- sum(c.amount) as amount
- from charges c
- where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
- and c.state = 'normal'
- and c.kind in ('accident')
- group by 1,2) A
- group by 1,2) B on A.Week = B.week and A.year = B.year
- left join
- (select
- weekofyear(c.created_at + interval 8 hour) as week,
- year(c.created_at + interval 8 hour) as year,
- sum(c.amount) as insurance
- from charges c
- where c.kind in ('insurance')
- and c.state = 'normal'
- and (c.created_at + interval 8 hour) >= @startdate
- group by 1,2) I on I.week = rm.week and I.year = rm.year
- left join
- (select
- A.week as week,
- A.year as year,
- A.Paid_subs as Paid_subs,
- round(Sum(A.amount),0) as subscriptionFee
- from
- (
- select
- weekofyear(c.created_at + interval '8' hour) as week,
- year(c.created_at + interval '8' hour) as year,
- c.id as cid,
- count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
- sum(c.amount) as amount
- from charges c
- where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
- and c.state = 'normal'
- and c.kind in ('subscriptionFee')
- group by 1,2) A
- group by 1,2) C
- on A.week = C.week and A.year = C.year
- group by rm.week, rm.year ;
- ######## monthly Revenue Related Info
- set @startDate = "2019-11-01 00:00:00";
- select
- rm.month as month,
- sum(rm.nr) as netrevenues,
- sum(rm.rent) as rental,
- sum(rm.d2d) as d2d,
- sum(O.oneway_amount) as oneway,
- sum(C.subscriptionFee) as subscriptionFee,
- sum(rm.mileage) as mileage,
- sum(A.Amount) as penalty_Repair,
- sum(rm.paid_coupon) as Coupon,
- IFNULL(B.noa,0) as Accidents,
- IFNULL(B.accidentamount,0) as accidentamount,
- IFNULL(sum(I.insurance),0) as insurance,
- rm.year
- from
- (select
- month(r.return_at + interval '8' hour) as month,
- year(r.return_at + interval '8' hour) as year,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent')),0)) as rent,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
- sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
- sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
- round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance')),0)- ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
- from reservations r, members m
- where r.member_id=m.id
- and m.imaginary in ('normal','sofam')
- and r.state = 'completed'
- and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
- group by 1,2
- ) rm
- left join
- ######### monthly other amounts
- (select
- month(c.created_at + interval '8' hour) as month,
- year(c.created_at + interval '8' hour) as year,
- sum(c.amount) as amount
- from charges c
- where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
- and c.state = 'normal'
- and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee','insurance')
- group by 1,2) A on A.month = rm.month and A.year = rm.year
- left join
- (select
- A.month as month,
- A.year as year,
- A.noa as noa,
- round(Sum(A.amount),0) as accidentamount
- from
- (
- select
- month(c.created_at + interval '8' hour) as month,
- year(c.created_at + interval '8' hour) as year,
- c.id as cid,
- count(distinct case when c.kind = 'accident' then c.id end) as noa,
- sum(c.amount) as amount
- from charges c
- where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
- and c.state = 'normal'
- and c.kind in ('accident')
- group by 1,2) A
- group by 1,2) B
- on A.month = B.month and A.year = B.year
- left join
- (select
- month(c.created_at + interval 8 hour) as month,
- year(c.created_at + interval 8 hour) as year,
- sum(c.amount) as oneway_amount
- from charges c
- where c.kind in ('oneway')
- and c.state = 'normal'
- and (c.created_at + interval 8 hour) >= @startdate
- group by 1,2) O on O.month = rm.month and O.year = rm.year
- left join
- (select
- month(c.created_at + interval 8 hour) as month,
- year(c.created_at + interval 8 hour) as year,
- sum(c.amount) as insurance
- from charges c
- where c.kind in ('insurance')
- and c.state = 'normal'
- and (c.created_at + interval 8 hour) >= @startdate
- group by 1,2) I on I.month = rm.month and I.year = rm.year
- left join
- (select
- A.month as month,
- A.year as year,
- A.Paid_subs as Paid_subs,
- round(Sum(A.amount),0) as subscriptionFee
- from
- (
- select
- month(c.created_at + interval '8' hour) as month,
- year(c.created_at + interval '8' hour) as year,
- c.id as cid,
- count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
- sum(c.amount) as amount
- from charges c
- where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
- and c.state = 'normal'
- and c.kind in ('subscriptionFee')
- group by 1,2) A
- group by 1,2) C
- on rm.month = C.month and rm.year = C.year
- group by rm.month,rm.year;
Advertisement
Add Comment
Please, Sign In to add comment