ahmedrahil786

KID - Revenues- Divided by Year

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