ahmedrahil786

City and age wise - MFT split - Rahil

Jun 8th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. set @start = "2019-01-01 00:00:00";
  2. select A.week as week, A.year as year, B.city as city, count(A.mid) as totalmfts,
  3. count(distinct case when B.age >= 19 and B.age <= 23 then A.mid end) as 19_23,
  4. count(distinct case when B.age > 23 and B.age <= 27 then A.mid end) as 24_27,
  5. count(distinct case when B.age > 27 and B.age <= 32 then A.mid end) as 28_32,
  6. count(distinct case when B.age > 32 and B.age <= 37 then A.mid end) as 33_37,
  7. count(distinct case when B.age > 37 and B.age <= 42 then A.mid end) as 38_42,
  8. count(distinct case when B.age > 42 then A.mid end) as 44_plus,
  9. ROUND((count(distinct case when B.age >= 19 and B.age <= 23 then A.mid end)/count(A.mid))*100,2) as 19_23_PCT,
  10. ROUND((count(distinct case when B.age > 23 and B.age <= 27 then A.mid end)/count(A.mid))*100,2) as 24_27_PCT,
  11. ROUND((count(distinct case when B.age > 27 and B.age <= 32 then A.mid end)/count(A.mid))*100,2) as 28_32_PCT,
  12. ROUND((count(distinct case when B.age > 32 and B.age <= 37 then A.mid end)/count(A.mid))*100,2) as 33_37_PCT,
  13. ROUND((count(distinct case when B.age > 37 and B.age <= 42 then A.mid end)/count(A.mid))*100,2) as 38_42_PCT,
  14. ROUND((count(distinct case when B.age > 42 then A.mid end)/count(A.mid))*100,2) as 44_plus_PCT
  15.  
  16. from
  17. (select distinct c.member_id as mid, weekofyear(c.created_at + interval '8' hour) as week, year(c.created_at + interval '8' hour) as year
  18. from charges c
  19. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  20. left join
  21. (select distinct r.member_id as mid, (YEAR(r.start_at) - YEAR(m.birthday)) as age , z.city as city,
  22. min(r.id) as rid
  23. from reservations r
  24. join members m on r.member_id = m.id
  25. join zones z on r.start_zone_id = z.id
  26. where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
  27. group by r.member_id) B
  28. on A.mid = B.mid
  29. where B.city in ('KL','SL','PG','JB')
  30. group by 1,2,3
  31. order by 2 asc , 1 asc
Advertisement
Add Comment
Please, Sign In to add comment