ahmedrahil786

WIP Tracker - MFTs Redeems Applies - Version 2.0

Sep 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. set @startdate1 = '2019-5-1 00:00';
  2.  
  3. select
  4. E.*,
  5. IFNULL(F.MFT,0) as MFT
  6. from
  7. (select
  8. weekofyear(c.updated_at + interval '8' hour) as week,
  9. c.coupon_policy_id as cpid,
  10. c.comment as couponname,
  11. count(c.activated_at + interval 8 hour) as TotalApplied,
  12. count(c.used_at + interval 8 hour) as totalredeems
  13. from coupons c
  14. where c.state = 'normal'
  15. and (c.updated_at + interval '8' hour) >= @startdate1
  16. group by 1,2
  17. order by 1 desc) E
  18. left join
  19. (select
  20. A.week as week,
  21. B.cpid as cpid,
  22. count(A.mid) as MFT
  23. from
  24. (Select P.mid as mid, Q.rid as rid , P.week as week from
  25. (select
  26. distinct ch.member_id as mid,
  27. weekofyear(ch.created_at + interval '8' hour) as week
  28. from charges ch
  29. where ch.created_at + interval '8' hour >= @startdate1
  30. and ch.kind = 'subscriptionFee'
  31. group by ch.member_id) P
  32. left join
  33. (select distinct r.member_id as mid , min(r.id) as rid
  34. from reservations r
  35. where r.state in ('completed','inUse')
  36. group by 1) Q
  37. on P.mid = Q.mid
  38. where Q.rid is not null) A
  39. join
  40. (select
  41. distinct c.reservation_id as rid,
  42. c.coupon_policy_id as cpid,
  43. c.comment
  44. from coupons c
  45. where c.used_at is not null
  46. group by 1,2
  47. order by 1 asc) B
  48. on B.rid = A.rid
  49. group by B.cpid,A.week
  50. order by A.week,B.cpid Desc) F
  51. on E.cpid = F.cpid and E.week = F.week
  52. where E.week >= 26
  53. order by E.week desc
Advertisement
Add Comment
Please, Sign In to add comment