Advertisement
ahmedrahil786

Key Index Dashboard - Revenues - V 2.0

Aug 25th, 2019
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 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(O.oneway_amount) as oneway,
  11. sum(C.subscriptionFee) as subscriptionFee,
  12. sum(rm.mileage) as mileage,
  13. sum(A.Amount) as penalty_Repair,
  14. sum(rm.paid_coupon) as Coupon,
  15. IFNULL(B.noa,0) as Accidents,
  16. IFNULL(B.accidentamount,0) 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','insurance')),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','insurance')),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. weekofyear(r.return_at + interval 8 hour) as week,
  50. sum(c.amount) as oneway_amount
  51. from reservations r
  52. join charges c on c.reservation_id = r.id
  53. where r.way in ('oneway', 'onewayReturn')
  54. and r.state = 'completed'
  55. and (r.return_at + interval 8 hour) >= @startdate
  56. group by 1) O on O.week = rm.week
  57. left join
  58. (select
  59. A.week as week,
  60. A.noa as noa,
  61. round(Sum(A.amount),0) as accidentamount
  62. from
  63. (
  64. select
  65. weekofyear(c.created_at + interval '8' hour) as week,
  66. c.id as cid,
  67. count(distinct case when c.kind = 'accident' then c.id end) as noa,
  68. sum(c.amount) as amount
  69. from charges c
  70. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  71. and c.state = 'normal'
  72. and c.kind in ('accident')
  73. group by 1) A
  74. group by 1,2) B on A.Week = B.week
  75. left join
  76. (select
  77. A.week as week,
  78. A.Paid_subs as Paid_subs,
  79. round(Sum(A.amount),0) as subscriptionFee
  80. from
  81. (
  82. select
  83. weekofyear(c.created_at + interval '8' hour) as week,
  84. c.id as cid,
  85. count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
  86. sum(c.amount) as amount
  87. from charges c
  88. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  89. and c.state = 'normal'
  90. and c.kind in ('subscriptionFee')
  91. group by 1) A
  92. group by 1,2) C
  93. on A.week = C.week
  94. group by rm.week ;
  95.  
  96. ######## monthly Revenue Related Info
  97.  
  98. select
  99. rm.month as month,
  100. sum(rm.nr) as netrevenues,
  101. sum(rm.rent) as rental,
  102. sum(rm.d2d) as d2d,
  103. sum(O.oneway_amount) as oneway,
  104. sum(C.subscriptionFee) as subscriptionFee,
  105. sum(rm.mileage) as mileage,
  106. sum(A.Amount) as penalty_Repair,
  107. sum(rm.paid_coupon) as Coupon,
  108. IFNULL(B.noa,0) as Accidents,
  109. IFNULL(B.accidentamount,0) as accidentamount
  110.  
  111. from
  112. (select
  113. month(r.return_at + interval '8' hour) as month,
  114. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','d2d','oneway','mileage','insurance')),0)) as rent,
  115. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
  116. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
  117. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
  118. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
  119. sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
  120. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance')),0)- ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  121. from reservations r, members m
  122.  
  123. where r.member_id=m.id
  124. and m.imaginary in ('normal','sofam')
  125. and r.state = 'completed'
  126. and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
  127. group by 1
  128. ) rm
  129. left join
  130. ######### monthly other amounts
  131. (select
  132. month(c.created_at + interval '8' hour) as month,
  133. sum(c.amount) as amount
  134. from charges c
  135. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  136. and c.state = 'normal'
  137. and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee')
  138. group by 1) A on A.month = rm.month
  139. left join
  140. (select
  141. A.month as month,
  142. A.noa as noa,
  143. round(Sum(A.amount),0) as accidentamount
  144. from
  145. (
  146. select
  147. month(c.created_at + interval '8' hour) as month,
  148. c.id as cid,
  149. count(distinct case when c.kind = 'accident' then c.id end) as noa,
  150. sum(c.amount) as amount
  151. from charges c
  152. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  153. and c.state = 'normal'
  154. and c.kind in ('accident')
  155. group by 1) A
  156. group by 1,2) B
  157. on A.month = B.month
  158. left join
  159. (select
  160. month(r.return_at + interval 8 hour) as month,
  161. sum(c.amount) as oneway_amount
  162. from reservations r
  163. join charges c on c.reservation_id = r.id
  164. where r.way in ('oneway', 'onewayReturn')
  165. and r.state = 'completed'
  166. and (r.return_at + interval 8 hour) >= @startdate
  167. group by 1) O on O.month = A.month
  168. left join
  169. (select
  170. A.month as month,
  171. A.Paid_subs as Paid_subs,
  172. round(Sum(A.amount),0) as subscriptionFee
  173. from
  174. (
  175. select
  176. month(c.created_at + interval '8' hour) as month,
  177. c.id as cid,
  178. count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
  179. sum(c.amount) as amount
  180. from charges c
  181. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  182. and c.state = 'normal'
  183. and c.kind in ('subscriptionFee')
  184. group by 1) A
  185. group by 1,2) C
  186. on A.month = C.month
  187. group by rm.month
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement