ahmedrahil786

2 queries - Coupon policy ID and Promo Code ID

Nov 7th, 2019
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. ############ PROMO CODE ID
  2.  
  3. set @startdate1 = '2019-07-01';
  4. set @startdate2 = '2019-08-21';
  5. use socar_malaysia;
  6. Select L.week as week , L.pcid as pcid, Sum(L.mft) as MFTs from
  7. (select A.week, A.rid2 as rid, IFNULL(A.pcid,0) as pcid, if(rid2=firstRes,1,0) as mft from
  8. (select
  9. distinct r.id as rid2, r.member_id as mid,IFNULL(co.comment,"") as coupon, co.promo_code_id as pcid,
  10. weekofyear(r.start_at + interval '8' hour) as week,
  11. ifnull((select min(id) from reservations where state='completed' and r.member_id=member_id group by member_id),0) as firstRes
  12. from reservations r
  13. left join members m on r.member_id = m.id
  14. left join coupons co on co.reservation_id = r.id
  15. left join promo_codes pc on co.coupon_policy_id = pc.coupon_policy_id
  16. where r.state in ('completed','inUse')
  17. and (r.start_at + interval '8' hour) >= @startdate1
  18. and m.imaginary in ('sofam', 'normal')
  19. and r.member_id not in ('125', '127')
  20. group by rid2) A) L
  21. Group by 1,2 ;
  22.  
  23. ################# COUPON NAME
  24.  
  25. set @startdate1 = '2019-07-01';
  26. use socar_malaysia;
  27. Select L.week as week , L.coupon as coupon, L.pcid as pcid, Sum(L.mft) as MFTs from
  28. (select A.week, A.rid2 as rid, IFNULL(A.coupon,0) as coupon, IFNULL(A.pcid,0) as pcid, if(rid2=firstRes,1,0) as mft from
  29. (select
  30. distinct r.id as rid2, r.member_id as mid,IFNULL(co.comment,"") as coupon, co.promo_code_id as pcid,
  31. weekofyear(r.start_at + interval '8' hour) as week,
  32. ifnull((select min(id) from reservations where state='completed' and r.member_id=member_id group by member_id),0) as firstRes
  33. from reservations r
  34. left join members m on r.member_id = m.id
  35. left join coupons co on co.reservation_id = r.id
  36. left join promo_codes pc on co.coupon_policy_id = pc.coupon_policy_id
  37. where r.state in ('completed','inUse')
  38. and (r.start_at + interval '8' hour) >= @startdate1
  39. and m.imaginary in ('sofam', 'normal')
  40. and r.member_id not in ('125', '127')
  41. group by rid2) A) L
  42. Group by 1,2
Advertisement
Add Comment
Please, Sign In to add comment