ahmedrahil786

Becca Query to check number of active coupon codes from tim

Mar 29th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. select
  2. c.coupon_policy_id,
  3. c.comment,
  4. count(distinct r.id),
  5. sum(timestampdiff(hour, r.occupy_start_at, r.occupy_end_at)),
  6. IFNULL(sum(A.Paid),'') as Paidspend,
  7. IFNULL(sum(A.Promo),'') as Promospend,
  8. cp.requester,
  9. cp.requester_team
  10.  
  11. from coupons c
  12.  
  13. join reservations r
  14. on r.id = c.reservation_id
  15. join coupon_policies cp on cp.id = c.coupon_policy_id
  16.  
  17. join (
  18. select
  19. p.reservation_id as rid,
  20. sum(case when p.paid_type = 'card' then. p.amount end) as Paid,
  21. sum(case when p.paid_type = 'coupon' then p.amount end) as Promo
  22.  
  23. from payments p
  24.  
  25. group by p.reservation_id) A
  26. on A.rid = r.id
  27.  
  28. where r.start_at + interval '8' hour >= '2019-1-1'
  29. and r.start_at + interval '8' hour < '2019-12-31'
  30.  
  31.  
  32. and r.state = 'completed'
  33.  
  34. group by 1;
Advertisement
Add Comment
Please, Sign In to add comment