ahmedrahil786

New Key Index Dashboard- Revenues - Rahil

Dec 3rd, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 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. IFNULL(sum(I.insurance),0) as insurance
  18.  
  19. from
  20. (select
  21. weekofyear(r.return_at + interval '8' hour) as week,
  22. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent')),0)) as rent,
  23. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
  24. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
  25. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
  26. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
  27. sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
  28. 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
  29. from reservations r, members m
  30.  
  31. where r.member_id=m.id
  32. and m.imaginary in ('normal','sofam')
  33. and r.state = 'completed'
  34. and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
  35. group by 1
  36. ) rm
  37. left join
  38.  
  39. ######### Weekly other amounts
  40. (select
  41. weekofyear(c.created_at + interval '8' hour) as week,
  42. sum(c.amount) as amount
  43. from charges c
  44. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  45. and c.state = 'normal'
  46. and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee','insurance')
  47. group by 1) A on A.week = rm.week
  48. left join
  49. (select
  50. weekofyear(c.created_at + interval 8 hour) as week,
  51. sum(c.amount) as oneway_amount
  52. from charges c
  53. where c.kind in ('oneway')
  54. and c.state = 'normal'
  55. and (c.created_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. weekofyear(c.created_at + interval 8 hour) as week,
  78. sum(c.amount) as insurance
  79. from charges c
  80. where c.kind in ('insurance')
  81. and c.state = 'normal'
  82. and (c.created_at + interval 8 hour) >= @startdate
  83. group by 1) I on I.week = rm.week
  84. left join
  85. (select
  86. A.week as week,
  87. A.Paid_subs as Paid_subs,
  88. round(Sum(A.amount),0) as subscriptionFee
  89. from
  90. (
  91. select
  92. weekofyear(c.created_at + interval '8' hour) as week,
  93. c.id as cid,
  94. count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
  95. sum(c.amount) as amount
  96. from charges c
  97. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  98. and c.state = 'normal'
  99. and c.kind in ('subscriptionFee')
  100. group by 1) A
  101. group by 1,2) C
  102. on A.week = C.week
  103. group by rm.week ;
  104.  
  105. ######## monthly Revenue Related Info
  106.  
  107.  
  108. set @startDate = "2019-01-01 00:00:00";
  109.  
  110. select
  111. rm.month as month,
  112. sum(rm.nr) as netrevenues,
  113. sum(rm.rent) as rental,
  114. sum(rm.d2d) as d2d,
  115. sum(O.oneway_amount) as oneway,
  116. sum(C.subscriptionFee) as subscriptionFee,
  117. sum(rm.mileage) as mileage,
  118. sum(A.Amount) as penalty_Repair,
  119. sum(rm.paid_coupon) as Coupon,
  120. IFNULL(B.noa,0) as Accidents,
  121. IFNULL(B.accidentamount,0) as accidentamount,
  122. IFNULL(sum(I.insurance),0) as insurance
  123.  
  124. from
  125. (select
  126. month(r.return_at + interval '8' hour) as month,
  127. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent')),0)) as rent,
  128. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
  129. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
  130. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
  131. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
  132. sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
  133. 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
  134. from reservations r, members m
  135.  
  136. where r.member_id=m.id
  137. and m.imaginary in ('normal','sofam')
  138. and r.state = 'completed'
  139. and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
  140. group by 1
  141. ) rm
  142. left join
  143. ######### monthly other amounts
  144. (select
  145. month(c.created_at + interval '8' hour) as month,
  146. sum(c.amount) as amount
  147. from charges c
  148. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  149. and c.state = 'normal'
  150. and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee','insurance')
  151. group by 1) A on A.month = rm.month
  152. left join
  153. (select
  154. A.month as month,
  155. A.noa as noa,
  156. round(Sum(A.amount),0) as accidentamount
  157. from
  158. (
  159. select
  160. month(c.created_at + interval '8' hour) as month,
  161. c.id as cid,
  162. count(distinct case when c.kind = 'accident' then c.id end) as noa,
  163. sum(c.amount) as amount
  164. from charges c
  165. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  166. and c.state = 'normal'
  167. and c.kind in ('accident')
  168. group by 1) A
  169. group by 1,2) B
  170. on A.month = B.month
  171. left join
  172. (select
  173. month(c.created_at + interval 8 hour) as month,
  174. sum(c.amount) as oneway_amount
  175. from charges c
  176. where c.kind in ('oneway')
  177. and c.state = 'normal'
  178. and (c.created_at + interval 8 hour) >= @startdate
  179. group by 1) O on O.month = rm.month
  180. left join
  181. (select
  182. month(c.created_at + interval 8 hour) as month,
  183. sum(c.amount) as insurance
  184. from charges c
  185. where c.kind in ('insurance')
  186. and c.state = 'normal'
  187. and (c.created_at + interval 8 hour) >= @startdate
  188. group by 1) I on I.month = rm.month
  189. left join
  190. (select
  191. A.month as month,
  192. A.Paid_subs as Paid_subs,
  193. round(Sum(A.amount),0) as subscriptionFee
  194. from
  195. (
  196. select
  197. month(c.created_at + interval '8' hour) as month,
  198. c.id as cid,
  199. count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
  200. sum(c.amount) as amount
  201. from charges c
  202. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  203. and c.state = 'normal'
  204. and c.kind in ('subscriptionFee')
  205. group by 1) A
  206. group by 1,2) C
  207. on rm.month = C.month
  208. group by rm.month
Advertisement
Add Comment
Please, Sign In to add comment