ahmedrahil786

Coupon Tracking by policy ID for Shanyi - Rahil

Nov 7th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. set @startdate1 = '2019-5-1 00:00';
  2.  
  3. select
  4. E.*,
  5. IFNULL(F.MFTs,0) as MFT
  6. from
  7. (select
  8. weekofyear(c.updated_at + interval '8' hour) as week,c.promo_code_id as pcid,pc.code as pc,c.coupon_policy_id as cpid,c.comment as couponname,count(c.activated_at + interval 8 hour) as TotalApplied,
  9. count(c.used_at + interval 8 hour) as totalredeems
  10. from coupons c, promo_codes pc
  11. where c.state = 'normal'
  12. and c.promo_code_id=pc.id
  13. and c.coupon_policy_id = pc.coupon_policy_id
  14. and (c.updated_at + interval '8' hour) >= @startdate1
  15. #and c.promo_code_id in (641,642,643)
  16. group by 1,2
  17. order by 1 desc) E
  18. left join
  19. (Select L.week as week , L.pcid as pcid, Sum(L.mft) as MFTs from
  20. (select A.week, A.rid2 as rid, IFNULL(A.pcid,0) as pcid, if(rid2=firstRes,1,0) as mft from
  21. (select
  22. distinct r.id as rid2, r.member_id as mid,IFNULL(co.comment,"") as coupon, co.promo_code_id as pcid,
  23. weekofyear(r.start_at + interval '8' hour) as week,
  24. ifnull((select min(id) from reservations where state='completed' and r.member_id=member_id group by member_id),0) as firstRes
  25. from reservations r
  26. left join members m on r.member_id = m.id
  27. left join coupons co on co.reservation_id = r.id
  28. left join promo_codes pc on co.coupon_policy_id = pc.coupon_policy_id
  29. where r.state in ('completed','inUse')
  30. and (r.start_at + interval '8' hour) >= @startdate1
  31. and m.imaginary in ('sofam', 'normal')
  32. and r.member_id not in ('125', '127')
  33. group by rid2) A) L
  34. Group by 1,2) F
  35. on E.pcid = F.pcid and E.week = F.week
  36. where E.week >= 26
  37. #AND E.pcid in ('641','642','643')
  38. AND E.pc like 'FREE4%'
  39. group by E.week
  40. order by E.week desc, E.TotalApplied DESC
Advertisement
Add Comment
Please, Sign In to add comment