ahmedrahil786

Key Index Dashboard - Weekly and Monthly - Revenues

Apr 23rd, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. ######## weekly Revenue Related Info
  2.  
  3. set @startDate = "2019-01-01 00:00:00";
  4.  
  5. select
  6. rm.week as week,
  7. sum(rm.nr) as netrevenues,
  8. sum(rm.rent) as rental,
  9. sum(rm.d2d) as d2d,
  10. sum(rm.oneway) as oneway,
  11. sum(rm.mileage) as mileage,
  12. sum(rm.subscriptionFee) as subscriptionFee,
  13. sum(A.Amount) as penalty_Repair,
  14. sum(rm.paid_coupon) as Coupon,
  15. (B.noa) as Accidents,
  16. (B.accidentamount) as accidentamount
  17.  
  18. from
  19. (select
  20. weekofyear(r.return_at + interval '8' hour) as week,
  21. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','d2d','oneway','mileage')),0)) as rent,
  22. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
  23. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
  24. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
  25. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
  26. sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
  27. round(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)),2) as nr
  28. from reservations r, members m
  29.  
  30. where r.member_id=m.id
  31. and m.imaginary in ('normal','sofam')
  32. and r.state = 'completed'
  33. and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
  34. group by 1
  35. ) rm
  36. left join
  37.  
  38. ######### Weekly other amounts
  39. (select
  40. weekofyear(c.created_at + interval '8' hour) as week,
  41. sum(c.amount) as amount
  42. from charges c
  43. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  44. and c.state = 'normal'
  45. and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee')
  46. group by 1) A on A.week = rm.week
  47. left join
  48. (select
  49. A.week as week,
  50. A.noa as noa,
  51. round(Sum(A.amount),0) as accidentamount
  52. from
  53. (
  54. select
  55. weekofyear(c.created_at + interval '8' hour) as week,
  56. c.id as cid,
  57. count(distinct case when c.kind = 'accident' then c.id end) as noa,
  58. sum(c.amount) as amount
  59. from charges c
  60. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  61. and c.state = 'normal'
  62. and c.kind in ('accident')
  63. group by 1) A
  64. group by 1,2) B on A.Week = B.week
  65. group by rm.week ;
  66.  
  67. ######## monthly Revenue Related Info
  68.  
  69. select
  70. rm.month as month,
  71. sum(rm.nr) as netrevenues,
  72. sum(rm.rent) as rental,
  73. sum(rm.d2d) as d2d,
  74. sum(rm.oneway) as oneway,
  75. sum(rm.subscriptionFee) as subscriptionFee,
  76. sum(rm.mileage) as mileage,
  77. sum(A.Amount) as penalty_Repair,
  78. sum(rm.paid_coupon) as Coupon,
  79. (B.noa) as Accidents,
  80. (B.accidentamount) as accidentamount
  81.  
  82. from
  83. (select
  84. month(r.return_at + interval '8' hour) as month,
  85. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','d2d','oneway','mileage')),0)) as rent,
  86. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
  87. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
  88. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
  89. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
  90. sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
  91. round(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)),2) as nr
  92. from reservations r, members m
  93.  
  94. where r.member_id=m.id
  95. and m.imaginary in ('normal','sofam')
  96. and r.state = 'completed'
  97. and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
  98. group by 1
  99. ) rm
  100. left join
  101. ######### monthly other amounts
  102. (select
  103. month(c.created_at + interval '8' hour) as month,
  104. sum(c.amount) as amount
  105. from charges c
  106. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  107. and c.state = 'normal'
  108. and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee')
  109. group by 1) A on A.month = rm.month
  110. left join
  111. (select
  112. A.month as month,
  113. A.noa as noa,
  114. round(Sum(A.amount),0) as accidentamount
  115. from
  116. (
  117. select
  118. month(c.created_at + interval '8' hour) as month,
  119. c.id as cid,
  120. count(distinct case when c.kind = 'accident' then c.id end) as noa,
  121. sum(c.amount) as amount
  122. from charges c
  123. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  124. and c.state = 'normal'
  125. and c.kind in ('accident')
  126. group by 1) A
  127. group by 1,2) B
  128. on A.month = B.month
  129. group by rm.month
  130. RAW Paste Data
Advertisement
Add Comment
Please, Sign In to add comment