ahmedrahil786

Mr.Speedy Invoicing Query

May 4th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. set @startdate1 = '2020-03-01';
  2. set @startdate2 = '2020-06-01';
  3. use socar_malaysia;
  4. select A.return_date as return_date,A.start_date as start_date, A.rid2 as rid, A.name as name, A.region as region,
  5. A.car_name as car_name, A.coupon as coupon, A.dur as duration, A.distance as distance,
  6. A.resv_hour as started_hour, A.age as age, IFNULL(E.charges,0) as gross_rev,
  7. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  8. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev,weekofyear(A.return_date) as week
  9. from
  10. (select
  11. distinct r.id as rid2, r.member_id as mid, z.region, z.city, cc.car_name,IFNULL(co.code,"") as coupon,r.way as way, if(ma.country = 'MYS','local','international') as member_region,m.display_name as name,
  12. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  13. 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,
  14. IFNULL(sum(ra.mileage),0) as distance,
  15. Date(r.return_at + interval '8' hour) as return_Date,
  16. hour(r.created_at + interval '8' hour) as created_hour,
  17. Date(r.start_at + interval '8' hour) as start_Date,
  18. hour(r.start_at + interval '8' hour) as resv_hour,
  19. hour(r.occupy_start_at + interval '8' hour) as occupy_start_hour,
  20. (YEAR(r.start_at) - YEAR(m.birthday)) as age
  21. from reservations r left join members m on r.member_id = m.id
  22. left join zones z on z.id = r.start_zone_id
  23. left join cars cr on cr.id = r.car_id
  24. left join car_classes cc on cc.id = cr.car_class_id
  25. left join coupons co on co.reservation_id = r.id
  26. left join reservation_appendixes ra on ra.reservation_id = r.id
  27. left join member_appendixes ma on ma.member_id = m.id
  28. where r.state in ('completed')
  29. and r.return_at + interval 8 hour >= @startdate1
  30. and r.return_at + interval 8 hour <= @startdate2
  31. and m.imaginary in ('sofam', 'normal')
  32. and r.member_id not in ('125', '127')
  33. and co.coupon_policy_id = '1013'
  34. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  35. group by rid2) A
  36. left join
  37. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  38. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  39. group by rid) E on A.rid2 = E.rid
  40. left join
  41. (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
  42. where p.state = 'normal' and p.paid_type = 'coupon'
  43. group by p.reservation_id) F on A.rid2 = F.rid
  44. where weekofyear(A.return_date) = 19
Advertisement
Add Comment
Please, Sign In to add comment