ahmedrahil786

MFTs and MAUs from a coupon - Rahil

Jan 20th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. set @startdate1 = '2020-01-01';
  2. set @startdate2 = '2020-12-01';
  3. Select Weekofyear(I.return_date) as week,
  4. count(distinct case when I.mft = 1 then I.mid end) as MFTs,
  5. count(distinct case when I.mft = 0 then I.mid end) as MAUs
  6. from
  7. (select A.return_date as return_date, A.rid2 as rid,A.mid as mid, A.dur as dur, A.policy_id,
  8. A.codes as codes, if(rid2=firstRes,1,0) as mft from
  9. (select
  10. distinct r.id as rid2, r.member_id as mid, c.code as codes,c.coupon_policy_id as policy_id,
  11. ifnull((select min(id) from reservations where state='completed' and r.member_id=member_id group by member_id),0) as firstRes,
  12. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  13. Date(r.return_at + interval '8' hour) as return_Date
  14. from reservations r left join members m on r.member_id = m.id
  15. left join coupons c on c.reservation_id = r.id
  16. where r.state in ('completed','inUse','reserved')
  17. and r.return_at + interval 8 hour >= @startdate1
  18. and r.return_at + interval 8 hour <= @startdate2
  19. and m.imaginary in ('sofam', 'normal')
  20. and r.member_id not in ('125', '127')
  21. group by rid2) A
  22. where A.policy_id in ('890')
  23. group by 2
  24. order by 1 asc) I
  25. group by 1 ;
Advertisement
Add Comment
Please, Sign In to add comment