ahmedrahil786

NSPH Check - Duration level - 0-1, 1-2, 2-3

Nov 19th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 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 < 12, L.rid, null)) as 05_12_hr,
  15. count(if(L.duration >= 12 and L.duration < 24, L.rid, null)) as 12_24_hr,
  16. count(if(L.duration >= 24 and L.duration < 48, L.rid, null)) as 24_48_hr,
  17. count(if(L.duration >= 48, L.rid, null)) as 48__hr ,
  18. ##### Net_rev
  19. Sum(if(L.duration < 1, L.net_rev, null)) as 00_01_hr_amount,
  20. Sum(if(L.duration >= 1 and L.duration < 2, L.net_rev, null)) as 01_02_hr_amount,
  21. Sum(if(L.duration >= 2 and L.duration < 3, L.net_rev, null)) as 02_03_hr_amount,
  22. Sum(if(L.duration >= 3 and L.duration < 4, L.net_rev, null)) as 03_04_hr_amount,
  23. Sum(if(L.duration >= 4 and L.duration < 5, L.net_rev, null)) as 04_05_hr_amount,
  24. Sum(if(L.duration >= 5 and L.duration < 12, L.net_rev, null)) as 05_12_hr_amount,
  25. Sum(if(L.duration >= 12 and L.duration < 24, L.net_rev, null)) as 12_24_hr_amount,
  26. Sum(if(L.duration >= 24 and L.duration < 48, L.net_rev, null)) as 24_48_hr_amount,
  27. Sum(if(L.duration >= 48, L.net_rev, null)) as 48__hr_amount,
  28. ##### Coupon_check
  29. Sum(if(L.duration < 1, L.crid, null)) as 00_01_hr_Coupon,
  30. Sum(if(L.duration >= 1 and L.duration < 2, L.crid, null)) as 01_02_hr_Coupon,
  31. Sum(if(L.duration >= 2 and L.duration < 3, L.crid, null)) as 02_03_hr_Coupon,
  32. Sum(if(L.duration >= 3 and L.duration < 4, L.crid, null)) as 03_04_hr_Coupon,
  33. Sum(if(L.duration >= 4 and L.duration < 5, L.crid, null)) as 04_05_hr_Coupon,
  34. Sum(if(L.duration >= 5 and L.duration < 12, L.crid, null)) as 05_12_hr_Coupon,
  35. Sum(if(L.duration >= 12 and L.duration < 24, L.crid, null)) as 12_24_hr_Coupon,
  36. Sum(if(L.duration >= 24 and L.duration < 48, L.crid, null)) as 24_48_hr_Coupon,
  37. Sum(if(L.duration >= 48, L.crid, null)) as 48__hr_Coupon,
  38. ##### Duration check
  39. Sum(if(L.duration < 1, L.duration, null)) as 00_01_hr_amount,
  40. Sum(if(L.duration >= 1 and L.duration < 2, L.duration, null)) as 01_02_hr_duration,
  41. Sum(if(L.duration >= 2 and L.duration < 3, L.duration, null)) as 02_03_hr_duration,
  42. Sum(if(L.duration >= 3 and L.duration < 4, L.duration, null)) as 03_04_hr_duration,
  43. Sum(if(L.duration >= 4 and L.duration < 5, L.duration, null)) as 04_05_hr_duration,
  44. Sum(if(L.duration >= 5 and L.duration < 12, L.duration, null)) as 05_12_hr_duration,
  45. Sum(if(L.duration >= 12 and L.duration < 24, L.duration, null)) as 12_24_hr_duration,
  46. Sum(if(L.duration >= 24 and L.duration < 48, L.duration, null)) as 24_48_hr_duration,
  47. Sum(if(L.duration >= 48, L.duration, null)) as 48__hr_duration
  48.  
  49. from
  50.  
  51. (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, A.crid as crid,
  52. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  53. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  54. (select
  55. distinct r.id as rid2, r.member_id as mid, count(distinct case when co.comment is not null then co.reservation_id end) as crid,
  56. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  57. 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,
  58. Date(r.return_at + interval '8' hour) as return_Date
  59. from reservations r left join members m on r.member_id = m.id
  60. left join reservation_appendixes ra on ra.reservation_id = r.id
  61. left join coupons co on co.reservation_id = r.id
  62. where r.state in ('completed')
  63. and r.return_at + interval 8 hour >= @startdate1
  64. and r.return_at + interval 8 hour <= @startdate2
  65. and m.imaginary in ('sofam', 'normal')
  66. and r.member_id not in ('125', '127')
  67. group by rid2) A
  68. left join
  69. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  70. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  71. group by rid) E on A.rid2 = E.rid
  72. left join
  73. (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
  74. where p.state = 'normal' and p.paid_type = 'coupon'
  75. group by p.reservation_id) F on A.rid2 = F.rid) L
  76. group by 1
  77. order by 1 desc
Advertisement
Add Comment
Please, Sign In to add comment