ahmedrahil786

Socar Points + Insurance - Heng

Dec 2nd, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. set @start := '2018-12-31 00:00';
  2.  
  3. Select A.year,A.month, Round(B.earn,0) as points_earned , Round(A.redeem,0) as points_Redemmed, Round(B.earn - A.redeem,0) as total_Remaining , ROUND(IFNULL(C.Insurance,""),2) as Insurance from
  4.  
  5. (select
  6. month(p.created_at + interval 8 hour) as month,
  7. Year(p.created_at + interval 8 hour) as year,
  8. sum(p.point) as redeem
  9. from points p
  10. where p.state = 'normal'
  11. and date(p.created_at + interval 8 hour) >= @start
  12. and p.type = 'redeem'
  13. group by 1,2) A
  14. left join
  15. (select
  16. month(p.created_at + interval 8 hour) as month,
  17. Year(p.created_at + interval 8 hour) as year,
  18. sum(p.point) as earn
  19. from points p
  20. where p.state = 'normal'
  21. and date(p.created_at + interval 8 hour) >= @start
  22. and p.type = 'earn'
  23. group by 1,2) B
  24. on A.month = B.month and A.year = B.year
  25. left join
  26. (select month(c.created_at + interval 8 hour) as month,
  27. year(c.created_at + interval 8 hour) as year,
  28. sum(c.amount) as Insurance
  29. from charges c
  30. left join charge_meta cm on cm.charge_id = c.id
  31. left join reservations r on r.id = c.reservation_id
  32. left join members m on m.id = c.member_id
  33. where c.kind = 'insurance'
  34. and date(c.created_at + interval 8 hour) >= @start
  35. and m.imaginary in ('sofam', 'normal')
  36. and r.member_id not in ('125', '127')
  37. and r.state in ('completed','inUse')
  38. and c.state = 'normal'
  39. group by 1,2) C
  40. on C.month = A.month and A.year = C.year
  41. order by 1,2 asc
Advertisement
Add Comment
Please, Sign In to add comment