ahmedrahil786

2- Key Index - Weekly and Monthly - Revenues + Accidents

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