case
1、例:
select u.uid,case when u.birthday>='1990' and u.birthday<='1999' then '90 后' when u.birthday>='1980' and u.birthday<='1989' then '80 后' when u.birthday>='1970' and u.birthday<='1979' then '70 后' when u.birthday>='1960' and u.birthday<='1969' then '60 后' when u.birthday>='1950' and u.birthday<='1959' then '50 后' else '其他' end as period from middle_bill_user u
运行
uid period 1 90 后 2 90 后 3 80 后 4 70 后 5 90 后 .....
再处理
select mbu.period, count(mbu.uid) as num from (select u.uid,case when u.birthday>='1990' and u.birthday<='1999' then '90 后' when u.birthday>='1980' and u.birthday<='1989' then '80 后' when u.birthday>='1970' and u.birthday<='1979' then '70 后' when u.birthday>='1960' and u.birthday<='1969' then '60 后' when u.birthday>='1950' and u.birthday<='1959' then '50 后' else '其他' end as period from middle_bill_user u) mbu group by mbu.period; '
结果
period num 90后 121 80后 20 ....