ahmedrahil786

Ryan Queries - Monthly All

Apr 26th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. # summary of Usage all (end_at) - Monthly collected
  2.  
  3.  
  4. use socar_malaysia;
  5. select
  6. r.id, r.member_id, m.first_name, m.last_name, m.email,
  7. (select dl.birthday
  8. from driver_licenses dl
  9. where m.id=dl.member_id
  10. order by dl.created_at desc limit 1) as birthday,
  11. (year(convert_tz(now(), '+00:00', '+8:00')) - substring(birthday, 1, 4)
  12. - if(substring(birthday, 5, 4) - date_format(convert_tz(now(), '+00:00', '+8:00'), "%m%d") > 0, 1, 0)) as age,
  13. (select dl.gender
  14. from driver_licenses dl
  15. where m.id=dl.member_id
  16. order by dl.created_at desc limit 1
  17. ) as gender, r.way, r.state, r.start_zone_id as zone_id, z.region as region, z.name as zone_name, r.car_id, c.registration_no, cs.car_name as cartype,
  18. convert_tz(r.created_at, '+00:00', '+8:00') as rc, convert_tz(r.start_at, '+00:00', '+8:00') as rs, convert_tz(r.end_at, '+00:00', '+8:00') as re, convert_tz(r.return_at, '+00:00', '+8:00') as rr,
  19. timestampdiff(hour, r.start_at, r.end_at) as dur,
  20. timestampdiff(hour, r.created_at, r.start_at) as buffer,
  21. timestampdiff(minute, r.end_at, r.return_at) as delay,
  22. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='rent'),0) as rent,
  23. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='d2d'),0) as d2d,
  24. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='oneway'),0) as oneway,
  25. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='mileage'),0) as mileage,
  26. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='penalty'),0) as penalty,
  27. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='accident'),0) as accident,
  28. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind not in ('rent','d2d','oneway','mileage','penalty','accident','subscriptionFee')),0) as etc,
  29. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','d2d','oneway','mileage')),0)
  30. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0) as nr,
  31. ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id),0) as paid,
  32. ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0) as paid_coupon
  33.  
  34. from reservations r, zones z, members m, car_classes cs, cars c
  35. where
  36. r.start_zone_id = z.id
  37. and r.member_id = m.id
  38. and c.id = r.car_id
  39. and c.car_class_id = cs.id
  40. and m.imaginary in ('sofam', 'normal')
  41. #and m.imaginary not in ('test')
  42. and r.member_id not in ('125', '127')
  43. and z.id not in (2, 3, 101)
  44. and r.state in ('completed')
  45. #and cs.id = '4'
  46. #and convert_tz(r.return_at, '+00:00', '+8:00') >= '2018-01-01= 00:00:00'
  47. and convert_tz(r.end_at, '+00:00', '+8:00') between '2019-03-01= 00:00:00' and '2019-03-31= 23:59:59'
  48.  
  49.  
  50.  
  51. group by r.id
  52. order by re
  53. ;
  54.  
  55. #created_at > '2017-11-20= 00:00:00'
  56. #and created_at <= '2017-11-28= 23:59:59'
  57. #group by m
  58.  
  59. ###########################################################################
  60.  
  61. ##########################################################################################################
  62. # summary of Usage all (end_at)
  63.  
  64.  
  65. use socar_malaysia;
  66. select
  67. r.id, r.member_id, m.first_name, m.last_name, m.email,
  68. (select dl.birthday
  69. from driver_licenses dl
  70. where m.id=dl.member_id
  71. order by dl.created_at desc limit 1) as birthday,
  72. (year(convert_tz(now(), '+00:00', '+8:00')) - substring(birthday, 1, 4)
  73. - if(substring(birthday, 5, 4) - date_format(convert_tz(now(), '+00:00', '+8:00'), "%m%d") > 0, 1, 0)) as age,
  74. (select dl.gender
  75. from driver_licenses dl
  76. where m.id=dl.member_id
  77. order by dl.created_at desc limit 1
  78. ) as gender, r.way, r.state, r.start_zone_id as zone_id, z.region as region, z.name as zone_name, r.car_id, c.registration_no, cs.car_name as cartype,
  79. convert_tz(r.created_at, '+00:00', '+8:00') as rc, convert_tz(r.start_at, '+00:00', '+8:00') as rs, convert_tz(r.end_at, '+00:00', '+8:00') as re, convert_tz(r.return_at, '+00:00', '+8:00') as rr,
  80. timestampdiff(hour, r.start_at, r.end_at) as dur,
  81. timestampdiff(hour, r.created_at, r.start_at) as buffer,
  82. timestampdiff(minute, r.end_at, r.return_at) as delay,
  83. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='rent'),0) as rent,
  84. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='d2d'),0) as d2d,
  85. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='oneway'),0) as oneway,
  86. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind='mileage'),0) as mileage,
  87. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind not in ('rent','d2d','oneway','mileage','subscriptionFee')),0) as etc,
  88. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','d2d','oneway','mileage')),0)
  89. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0) as nr,
  90. ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id),0) as paid,
  91. ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0) as paid_coupon
  92.  
  93. from reservations r, zones z, members m, car_classes cs, cars c
  94. where
  95. r.start_zone_id = z.id
  96. and r.member_id = m.id
  97. and c.id = r.car_id
  98. and c.car_class_id = cs.id
  99. and m.imaginary in ('sofam', 'normal')
  100. #and m.imaginary not in ('test')
  101. and r.member_id not in ('125', '127')
  102. and z.id not in (2, 3, 101)
  103. and r.state in ('completed')
  104. #and cs.id = '4'
  105. #and convert_tz(r.return_at, '+00:00', '+8:00') >= '2018-01-01= 00:00:00'
  106. and convert_tz(r.end_at, '+00:00', '+8:00') between '2018-01-01= 00:00:00' and '2018-11-30= 23:59:59'
  107.  
  108.  
  109.  
  110. group by r.id
  111. order by re
  112. ;
  113. #created_at > '2017-11-20= 00:00:00'
  114. #and created_at <= '2017-11-28= 23:59:59'
  115. #group by m
Advertisement
Add Comment
Please, Sign In to add comment