ahmedrahil786

Net Revenues + Reservations Breakdown by Dur

Nov 14th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. set @startdate1 = date_sub(CURDATE(), interval 6 month);
  2. set @startdate2 = date_add(CURDATE(), interval 10 day);
  3. use socar_malaysia;
  4.  
  5. select
  6. L.return_Date,
  7. count(L.rid) as total,
  8. sum(L.net_rev) as net_rev,
  9. count(if(L.duration < 1, L.rid, null)) as 00_01_hr,
  10. count(if(L.duration >= 1 and L.duration < 2, L.rid, null)) as 01_02_hr,
  11. count(if(L.duration >= 2 and L.duration < 3, L.rid, null)) as 02_03_hr,
  12. count(if(L.duration >= 3 and L.duration < 4, L.rid, null)) as 03_04_hr,
  13. count(if(L.duration >= 4 and L.duration < 5, L.rid, null)) as 04_05_hr,
  14. count(if(L.duration >= 5 and L.duration < 6, L.rid, null)) as 05_06_hr,
  15. count(if(L.duration >= 6 and L.duration < 7, L.rid, null)) as 06_07_hr,
  16. count(if(L.duration >= 7 and L.duration < 8, L.rid, null)) as 07_08_hr,
  17. count(if(L.duration >= 8 and L.duration < 9, L.rid, null)) as 08_09_hr,
  18. count(if(L.duration >= 9 and L.duration < 10, L.rid, null)) as 09_10_hr,
  19. count(if(L.duration >= 10 and L.duration < 11, L.rid, null)) as 10_11_hr,
  20. count(if(L.duration >= 11 and L.duration < 12, L.rid, null)) as 11_12_hr,
  21. count(if(L.duration >= 12 and L.duration < 24, L.rid, null)) as 12_24_hr,
  22. count(if(L.duration >= 24 and L.duration < 48, L.rid, null)) as 24_48_hr,
  23. count(if(L.duration >= 48, L.rid, null)) as 48__hr ,
  24. ##### Net_rev
  25. Sum(if(L.duration < 1, L.net_rev, null)) as 00_01_hr_amount,
  26. Sum(if(L.duration >= 1 and L.duration < 2, L.net_rev, null)) as 01_02_hr_amount,
  27. Sum(if(L.duration >= 2 and L.duration < 3, L.net_rev, null)) as 02_03_hr_amount,
  28. Sum(if(L.duration >= 3 and L.duration < 4, L.net_rev, null)) as 03_04_hr_amount,
  29. Sum(if(L.duration >= 4 and L.duration < 5, L.net_rev, null)) as 04_05_hr_amount,
  30. Sum(if(L.duration >= 5 and L.duration < 6, L.net_rev, null)) as 05_06_hr_amount,
  31. Sum(if(L.duration >= 6 and L.duration < 7, L.net_rev, null)) as 06_07_hr_amount,
  32. Sum(if(L.duration >= 7 and L.duration < 8, L.net_rev, null)) as 07_08_hr_amount,
  33. Sum(if(L.duration >= 8 and L.duration < 9, L.net_rev, null)) as 08_09_hr_amount,
  34. Sum(if(L.duration >= 9 and L.duration < 10, L.net_rev, null)) as 09_10_hr_amount,
  35. Sum(if(L.duration >= 10 and L.duration < 11, L.net_rev, null)) as 10_11_hr_amount,
  36. Sum(if(L.duration >= 11 and L.duration < 12, L.net_rev, null)) as 11_12_hr_amount,
  37. Sum(if(L.duration >= 12 and L.duration < 24, L.net_rev, null)) as 12_24_hr_amount,
  38. Sum(if(L.duration >= 24 and L.duration < 48, L.net_rev, null)) as 24_48_hr_amount,
  39. Sum(if(L.duration >= 48, L.net_rev, null)) as 48__hr_amount
  40.  
  41. from
  42.  
  43. (select A.return_date as return_date, A.rid2 as rid, A.dur as duration, A.ad_sdur as ad_sdur, IFNULL(E.charges,0) as gross_rev,
  44. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  45. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  46. (select
  47. distinct r.id as rid2, r.member_id as mid,
  48. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  49. 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,
  50. Date(r.return_at + interval '8' hour) as return_Date
  51. from reservations r left join members m on r.member_id = m.id
  52. left join reservation_appendixes ra on ra.reservation_id = r.id
  53. where r.state in ('completed')
  54. and r.return_at + interval 8 hour >= @startdate1
  55. and r.return_at + interval 8 hour <= @startdate2
  56. and m.imaginary in ('sofam', 'normal')
  57. and r.member_id not in ('125', '127')
  58. group by rid2) A
  59. left join
  60. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  61. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  62. group by rid) E on A.rid2 = E.rid
  63. left join
  64. (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
  65. where p.state = 'normal' and p.paid_type = 'coupon'
  66. group by p.reservation_id) F on A.rid2 = F.rid) L
  67. group by 1
  68. order by 1 desc
Advertisement
Add Comment
Please, Sign In to add comment