ahmedrahil786

Milage unpaid - Balances- Fuel product Project

Mar 10th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. set @startdate1 = '2019-06-01';
  2. set @startdate2 = '2020-03-11';
  3. use socar_malaysia;
  4. select year(A.return_date) as year, Month(A.return_date) as month, count(distinct A.rid2) as total_resv, count(M.rid) as Paid_Excess_mileage_resv, count(N.rid) as Unpaid_Excess_mileage_resv ,
  5. SUM(round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2)) as Total_net_rev , Sum(M.amount) as Paid_Mileage, Sum(N.amount) as Unpaid_mileage , Round((Sum(N.amount)/Sum(M.amount))*100,2) as PCT_Unpaid
  6. from
  7. (select
  8. distinct r.id as rid2, r.member_id as mid,
  9. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  10. 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,
  11. Date(r.return_at + interval '8' hour) as return_Date
  12. from reservations r left join members m on r.member_id = m.id
  13. where r.state in ('completed')
  14. and r.return_at + interval 8 hour >= @startdate1
  15. and r.return_at + interval 8 hour <= @startdate2
  16. and m.imaginary in ('sofam', 'normal')
  17. and r.member_id not in ('125', '127')
  18. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  19. group by rid2) A
  20. left join
  21. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  22. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  23. group by rid) E on A.rid2 = E.rid
  24. left join
  25. (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
  26. where p.state = 'normal' and p.paid_type = 'coupon'
  27. group by p.reservation_id) F on A.rid2 = F.rid
  28. left join
  29. (select c.reservation_id as rid, sum(c.amount) as amount from charges c
  30. where c.state='normal' and c.kind in ('mileage')
  31. group by rid) M on A.rid2 = M.rid
  32. left join
  33. (select c.reservation_id as rid, sum(c.balance) as amount from charges c
  34. where c.state= 'normal' and c.kind in ('mileage')
  35. and c.balance > '0'
  36. group by rid) N on A.rid2 = N.rid
  37. group by 1,2
  38. order by 1 asc , 2 asc
Advertisement
Add Comment
Please, Sign In to add comment