首页 > 解决方案 > 合并选择中的值

问题描述

我有下一部分查询:

SELECT types, count
FROM ...

结果如下:

types        count
soft         3
lite soft    2
middle soft  7
hard soft    2
other        5

我需要的是合并结果soft,lite soft和其他。得到下一张桌子:

types        count
middle soft  7
hard soft    2
other        9

标签: sqlpostgresqlgroup-bycountcase

解决方案


考虑一个对, then 聚合case进行转码的表达式:types

select 
    case when types in ('middle soft', 'hard soft') then types else 'other' end as new_types, 
    sum(count) cnt
from mytable
group by 1

推荐阅读