ahmedrahil786

Daily Index Query - Zone car Half

May 8th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. set @start := '2019-02-01 00:00:00';
  2. set @end := '2019-04-31 23:59:59';
  3.  
  4.  
  5. select
  6. cz.zid,
  7. cz.zname,
  8. cz.class,
  9. cz.ctype,
  10. cz.cid,
  11. #count(cz.ncars2) as cars2,
  12. cz.ncars,
  13. ifnull(sum(rm.nuse),0) as nuse,
  14. ifnull(sum(rm.rnd),0) as round,
  15. ifnull(sum(rm.d2d),0) as d2d,
  16. ifnull(sum(rm.ncar_used),0) as ncar_used,
  17. cz.nzone,
  18. ifnull(sum(rm.nmember),0) as memeber,
  19. ifnull(rm.rmil,0) as rmil,
  20. ifnull(sum(rm.dur),0) as dur,
  21. ifnull(sum(rm.revenues),0) as rrevenues,
  22. ifnull(sum(rm.coupons),0) as couponrev,
  23. ifnull(sum(rm.NR),0) as NR,
  24. ifnull(count(rm.MFT),0) as MFT,
  25. ifnull(sum(rm.ActiveMembers),0) as MAU
  26.  
  27.  
  28. from
  29. (select cz.zone_id as zid,
  30. z.region as region,
  31. z.city as team,
  32. z.name as zname,
  33. cz.car_id as cid,
  34. c.registration_no as plate,
  35. c.car_class_id as class,
  36. (select car_classes.car_name from car_classes where car_classes.id=c.car_class_id) as ctype,
  37. count(cz.id) as op_day,
  38. #count(z.id) as nzone,
  39. count(distinct case when z.state = 'normal' then z.id end) as nzone,
  40. count(cz.id) as ncars,
  41. #count(distinct case when cz.zone_state = 'normal' then cz.id end) as ncars2,
  42. date_sub(cz.log_date, interval 1 day) as czdate
  43. from car_zone_logs cz, zones z, cars c
  44. where cz.zone_id=z.id
  45. and cz.car_id=c.id
  46. and z.id not in (2,3,101,383,378,697,712,818)
  47. and date_sub(cz.log_date+interval 8 hour, interval 1 day) between (@start) and (@end)
  48. group by cid, zid
  49. order by cid
  50. )cz
  51. left join
  52. (select
  53. r.car_id as cid2,
  54. r.start_zone_id as zid2,
  55. count(r.id) as nuse,
  56. count(if(r.way='round', r.id, null)) as rnd,
  57. count(if(r.way='d2d', r.id, null)) as d2d,
  58. count(if(r.way='oneway', r.id, null)) as oneway,
  59. count(if(r.way='onewayreturn', r.id, null)) as onewayreturn,
  60. count(distinct case when r.state = 'completed' then r.member_id end) as ActiveMembers,
  61. count(distinct r.car_id) as ncar_used,
  62. count(distinct r.member_id) as nmember,
  63. ifnull(sum(ra.mileage),0) as rmil,
  64. sum(timestampdiff(hour, r.start_at, r.end_at)) as dur,
  65. ifnull((select count(distinct case when charges.kind = 'subscriptionFee' then charges.member_id end) from charges where member_id = m.id),0) as MFT,
  66. ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','d2d','mileage')),0) as rent,
  67. ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0) as paid_coupon,
  68. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)),2) as revenues,
  69. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  70. sum((ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)- ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0))) as NR,
  71. date_format(convert_tz(r.return_at, '+00:00', '+8:00'),'%Y-%m-%d') as czdate2
  72. from reservations r, members m , reservation_appendixes ra
  73. where
  74. r.member_id=m.id
  75. and r.id = ra.reservation_id
  76. and m.imaginary in ('normal','sofam')
  77. and r.way in ('round','d2d','oneway')
  78. and r.state = 'completed'
  79. and r.return_at+interval 8 hour between @start and @end
  80. group by cid2, zid2, czdate2
  81. order by cid2
  82. ) rm
  83. on ( cz.zid=rm.zid2 and cz.cid=rm.cid2)
  84. group by cz.zid, cz.cid
  85. order by cz.zid, op_day desc, cz.cid;
Advertisement
Add Comment
Please, Sign In to add comment