ahmedrahil786

Type of SCDW tagged to a reservation - Rahil

Mar 25th, 2020
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. set @startdate1 = '2019-11-01';
  2. set @startdate2 = '2020-02-31';
  3. use socar_malaysia;
  4.  
  5. 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,
  6. IFNULL(E.charges,0) as gross_rev, IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  7. 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
  8. from
  9. (select
  10. distinct r.id as rid2, r.member_id as mid,
  11. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  12. 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,
  13. IFNULL(sum(ra.mileage),0) as distance,
  14. Date(r.return_at + interval '8' hour) as return_Date,
  15. (YEAR(r.start_at) - YEAR(m.birthday)) as age
  16. from reservations r left join members m on r.member_id = m.id
  17. left join reservation_appendixes ra on ra.reservation_id = r.id
  18. where r.state in ('completed')
  19. and r.return_at + interval 8 hour >= @startdate1
  20. and r.return_at + interval 8 hour <= @startdate2
  21. and m.imaginary in ('sofam', 'normal')
  22. and r.member_id not in ('125', '127')
  23. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  24. group by rid2) A
  25. left join
  26. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  27. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  28. group by rid) E on A.rid2 = E.rid
  29. left join
  30. (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
  31. where p.state = 'normal' and p.paid_type = 'coupon'
  32. group by p.reservation_id) F on A.rid2 = F.rid
  33. left join
  34. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  35. where c.state='normal' and c.kind in ('insurance')
  36. group by rid) G on A.rid2 = G.rid
  37. left join
  38. (select c.reservation_id as rid ,
  39. 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
  40. from charges c
  41. left join charge_meta cm on cm.charge_id = c.id
  42. left join reservations r on r.id = c.reservation_id
  43. left join members m on m.id = c.member_id
  44. where c.kind = 'insurance'
  45. and m.imaginary in ('sofam', 'normal')
  46. and r.member_id not in ('125', '127')
  47. and r.state in ('completed','inUse')
  48. and c.state = 'normal'
  49. group by 1) H on H.rid = A.rid2
  50. group by 2
Advertisement
Add Comment
Please, Sign In to add comment