Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set @startdate1 = '2019-11-01';
- set @startdate2 = '2020-02-31';
- use socar_malaysia;
- select A.return_date as return_date, A.rid2 as rid, A.mid as mid, A.dur as dur, A.ad_sdur as ad_sdur, A.distance as distance, A.age as age,
- 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 , IFNULL(G.charges,0) as Insurance, IFNULL(H.type,"") as Insurance_Type
- from
- (select
- distinct r.id as rid2, r.member_id as mid,
- round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
- round(sum(floor(timestampdiff(minute, r.start_at, r.end_at)/60/24)*12+if(mod(timestampdiff(hour, r.start_at, r.end_at),24)>=12,12,mod(timestampdiff(minute, r.start_at, r.end_at)/60,24))),2) as ad_sdur,
- IFNULL(sum(ra.mileage),0) as distance,
- Date(r.return_at + interval '8' hour) as return_Date,
- (YEAR(r.start_at) - YEAR(m.birthday)) as age
- from reservations r left join members m on r.member_id = m.id
- left join reservation_appendixes ra on ra.reservation_id = r.id
- where r.state in ('completed')
- and r.return_at + interval 8 hour >= @startdate1
- and r.return_at + interval 8 hour <= @startdate2
- and m.imaginary in ('sofam', 'normal')
- and r.member_id not in ('125', '127')
- and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
- group by rid2) A
- 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')
- group by rid) E on A.rid2 = 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 A.rid2 = F.rid
- left join
- (select c.reservation_id as rid, sum(c.amount) as charges from charges c
- where c.state='normal' and c.kind in ('insurance')
- group by rid) G on A.rid2 = G.rid
- left join
- (select c.reservation_id as rid ,
- if(cm.meta LIKE '%Basic SCDW%','Basic_SCDW',if(cm.meta LIKE '%Standard SCDW%','Standard SCDW', if(cm.meta LIKE '%Premium SCDW%','Premium SCDW',""))) as type
- from charges c
- left join charge_meta cm on cm.charge_id = c.id
- left join reservations r on r.id = c.reservation_id
- left join members m on m.id = c.member_id
- where c.kind = 'insurance'
- and m.imaginary in ('sofam', 'normal')
- and r.member_id not in ('125', '127')
- and r.state in ('completed','inUse')
- and c.state = 'normal'
- group by 1) H on H.rid = A.rid2
- group by 2
Advertisement
Add Comment
Please, Sign In to add comment