ahmedrahil786

Loui Duration Query

Jul 10th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. set @start_at = '2019-03-01 00:00:00';
  2.  
  3. #Usage by rental hours in SL area
  4. select
  5. concat(rweek, " (",right(wfirst,5),"~",right(wlast,5),")") as rweek,
  6.  
  7. case
  8. when(region='Nilai') then 'Nilai'
  9. when(region='Cyberjaya') then 'Cyberjaya'
  10. when(region='Seri Kembangan') then 'Seri Kembangan'
  11. when(region='Subang Jaya') then 'Subang Jaya'
  12. when(region='Setia Alam') then 'Setia Alam'
  13. when(region='Shah Alam') then 'Shah Alam'
  14. when(region='Klang') then 'Klang'
  15. when(region='Sepang') then 'Sepang'
  16. when(region='kota kemuning') then 'kota kemuning'
  17. when(region='semenyih') then 'semenyih'
  18. when(region='Puchong') then 'Puchong'
  19. when(region='PJ - South') then 'PJ - South'
  20. when(region='Kajang') then 'Kajang'
  21. when(region='Bangi') then 'Bangi'
  22. when(region='Putrajaya') then 'Putrajaya'
  23. when(region='Seremban') then 'Seremban' end as region,
  24.  
  25. 00_01_hr,01_02_hr,02_03_hr, 03_04_hr, 04_05_hr, 05_06_hr,06_07_hr,07_08_hr,08_09_hr,09_10_hr,10_11_hr,11_12_hr,
  26. 12_24_hr,
  27. 24_48_hr,
  28. 48__hr,
  29. #sum(00_01_hr+01_05_hr+05_10_hr+10_12_hr+12_24_hr+24_48_hr+48__hr),
  30. total
  31. from (
  32. select
  33. rweek, region, min(date_format(rret,"%Y-%m-%d")) as wfirst, max(date_format(rret,"%Y-%m-%d")) as wlast,
  34. count(rid) as total,
  35. count(if(duration < 1, rid, null)) as 00_01_hr,
  36. count(if(duration >= 1 and duration < 2, rid, null)) as 01_02_hr,
  37. count(if(duration >= 2 and duration < 3, rid, null)) as 02_03_hr,
  38. count(if(duration >= 3 and duration < 4, rid, null)) as 03_04_hr,
  39. count(if(duration >= 4 and duration < 5, rid, null)) as 04_05_hr,
  40. count(if(duration >= 5 and duration < 6, rid, null)) as 05_06_hr,
  41. count(if(duration >= 6 and duration < 7, rid, null)) as 06_07_hr,
  42. count(if(duration >= 7 and duration < 8, rid, null)) as 07_08_hr,
  43. count(if(duration >= 8 and duration < 9, rid, null)) as 08_09_hr,
  44. count(if(duration >= 9 and duration < 10, rid, null)) as 09_10_hr,
  45. count(if(duration >= 10 and duration < 11, rid, null)) as 10_11_hr,
  46. count(if(duration >= 11 and duration < 12, rid, null)) as 11_12_hr,
  47. count(if(duration >= 12 and duration < 24, rid, null)) as 12_24_hr,
  48. count(if(duration >= 24 and duration < 48, rid, null)) as 24_48_hr,
  49. count(if(duration >= 48, rid, null)) as 48__hr
  50.  
  51. from (
  52. select z.region as region, r.id as rid, r.member_id as mid,
  53. (select dl.birthday
  54. from driver_licenses dl
  55. where
  56. m.id=dl.member_id
  57. order by dl.id desc limit 1
  58. ) as dlst_uploaded,
  59. r.start_at+interval 8 hour as rst, r.end_at+interval 8 hour as rend, r.return_at+interval 8 hour as rret,
  60. concat(year(r.return_at+interval 8 hour), "-", lpad(week(r.return_at+interval 8 hour, 1),2,"0")) as rweek,
  61. timestampdiff(minute, r.start_at, r.end_at)/60 as duration
  62. from reservations r, members m, zones z
  63. where
  64. r.state='completed'
  65. and r.start_zone_id=z.id
  66. and z.city='sl'
  67. and r.member_id=m.id
  68. and m.imaginary in ('normal', 'sofam')
  69. ######################################################3
  70. # period set
  71. and r.return_at+interval 8 hour >= @start_at
  72. and r.return_at+interval 8 hour < @start_at+interval 3 month
  73. and r.start_zone_id not in (2,3,101,383,378,712)
  74. ) temp
  75. group by region
  76. ) temp
  77. group by region
  78. ;
Advertisement
Add Comment
Please, Sign In to add comment