How to group by with a special condition

前端 未结 3 1893
旧巷少年郎
旧巷少年郎 2021-01-11 23:47

Currently when I issue this SQL, it gets the distinct username.

I have some distinct usernames, which represent groups, e.g. GRP_BSN.

I would li

3条回答
  •  渐次进展
    2021-01-12 00:08

    If you wanted to do it by putting small groups into one bucket, instead of by a particular name pattern, you could use:

    select (case when cnt > 100 then username else 'OTHER' end), sum(cnt) as cnt
    from (select username, count(*) as cnt
          from host
          where seq between 0 and 2000
          group by username
         ) t
    group by (case when cnt > 100 then username else 'OTHER' end)
    

提交回复
热议问题