ahmedrahil786

MFT by age group - Rahil

Jun 2nd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. set @startdate1 = date_sub(CURDATE(), interval 6 month);
  2. set @startdate2 = date_add(CURDATE(), interval 10 day);
  3. use socar_malaysia;
  4. select A.date as date, count(A.mid) as mid,
  5. count(distinct case when A.age >= 19 and A.age <= 23 then A.mid end) as 19_23,
  6. count(distinct case when A.age > 23 and A.age <= 27 then A.mid end) as 24_27,
  7. count(distinct case when A.age > 27 and A.age <= 32 then A.mid end) as 28_32,
  8. count(distinct case when A.age > 32 and A.age <= 37 then A.mid end) as 33_37,
  9. count(distinct case when A.age > 37 and A.age <= 42 then A.mid end) as 38_42,
  10. count(distinct case when A.age > 42 then A.mid end) as 44_plus,
  11. ROUND((count(distinct case when A.age >= 19 and A.age <= 23 then A.mid end)/count(A.mid))*100,2) as 19_23_PCT,
  12. ROUND((count(distinct case when A.age > 23 and A.age <= 27 then A.mid end)/count(A.mid))*100,2) as 24_27_PCT,
  13. ROUND((count(distinct case when A.age > 27 and A.age <= 32 then A.mid end)/count(A.mid))*100,2) as 28_32_PCT,
  14. ROUND((count(distinct case when A.age > 32 and A.age <= 37 then A.mid end)/count(A.mid))*100,2) as 33_37_PCT,
  15. ROUND((count(distinct case when A.age > 37 and A.age <= 42 then A.mid end)/count(A.mid))*100,2) as 38_42_PCT,
  16. ROUND((count(distinct case when A.age > 42 then A.mid end)/count(A.mid))*100,2) as 44_plus_PCT
  17.  
  18. from
  19. (select
  20. distinct c.member_id as mid,
  21. date(c.created_at + interval '8' hour) as date,
  22. (YEAR(c.created_at) - YEAR(m.birthday)) as age
  23. from charges c
  24. left join members m on m.id = c.member_id
  25. where c.created_at + interval '8' hour >= @startdate1
  26. and c.created_at + interval '8' hour <= @startdate2
  27. and c.kind = 'subscriptionFee'
  28. and m.imaginary in ('sofam', 'normal')
  29. group by 1) A
  30. group by 1
  31. order by 1 desc
Advertisement
Add Comment
Please, Sign In to add comment