ahmedrahil786

Reservations by age - Rahil

Jun 2nd, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 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.return_date as return_date, count(A.rid) as resv,
  5. count(distinct case when A.age >= 19 and A.age <= 23 then A.rid end) as 19_23,
  6. count(distinct case when A.age > 23 and A.age <= 27 then A.rid end) as 24_27,
  7. count(distinct case when A.age > 27 and A.age <= 32 then A.rid end) as 28_32,
  8. count(distinct case when A.age > 32 and A.age <= 37 then A.rid end) as 33_37,
  9. count(distinct case when A.age > 37 and A.age <= 42 then A.rid end) as 38_42,
  10. count(distinct case when A.age > 42 then A.rid end) as 44_plus,
  11. ROUND((count(distinct case when A.age >= 19 and A.age <= 23 then A.rid end)/count(A.rid))*100,2) as 19_23_PCT,
  12. ROUND((count(distinct case when A.age > 23 and A.age <= 27 then A.rid end)/count(A.rid))*100,2) as 24_27_PCT,
  13. ROUND((count(distinct case when A.age > 27 and A.age <= 32 then A.rid end)/count(A.rid))*100,2) as 28_32_PCT,
  14. ROUND((count(distinct case when A.age > 32 and A.age <= 37 then A.rid end)/count(A.rid))*100,2) as 33_37_PCT,
  15. ROUND((count(distinct case when A.age > 37 and A.age <= 42 then A.rid end)/count(A.rid))*100,2) as 38_42_PCT,
  16. ROUND((count(distinct case when A.age > 42 then A.rid end)/count(A.rid))*100,2) as 44_plus_PCT
  17.  
  18. from
  19. (select
  20. distinct r.id as rid, r.member_id as mid,
  21. Date(r.return_at + interval '8' hour) as return_Date,
  22. (YEAR(r.start_at) - YEAR(m.birthday)) as age
  23. from reservations r left join members m on r.member_id = m.id
  24. where r.state in ('completed')
  25. and r.return_at + interval 8 hour >= @startdate1
  26. and r.return_at + interval 8 hour <= @startdate2
  27. and m.imaginary in ('sofam', 'normal')
  28. and r.member_id not in ('125', '127')
  29. group by rid) A
  30. group by 1
  31. order by 1 desc
Advertisement
Add Comment
Please, Sign In to add comment