mysql 按某字段分组分箱聚合查询

SELECT cidnum1,count(*) as num from 
(
SELECT DISTINCT id,cidnum,
(CASE 
        WHEN 0<cidnum and cidnum<=1 THEN '1个公司'
        WHEN 1<cidnum and cidnum<=5 THEN '2-5个公司'
        WHEN 5<cidnum and cidnum<=10 THEN '6-10个公司'
        when 10<cidnum and cidnum<=50 then '11-50个公司'
        when 50<cidnum and cidnum<=100 then '51-100个公司'
        when 100<cidnum and cidnum<=500 then '101-500个公司'
        when 501<cidnum and cidnum<=1000 then '501-1000个公司'
        ELSE '1001个公司以上'
END) as cidnum1
FROM TABLE
where ***
) a
GROUP BY cidnum1
ORDER BY num desc

 

posted @ 2021-05-07 07:41  cup_leo  阅读(574)  评论(0编辑  收藏  举报