如何解决在选择中合并值
我有查询的下一部分:
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
解决方法
考虑使用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
,
您可以使用case
表达式`:
select (case when type in ('middle soft','hard soft') then type else 'other' end) as type,sum(count)
from t
group by type;
如果您希望结果按特定顺序排列-以other
结尾,则横向联接会有所帮助:
select v.type,sum(count)
from t cross join lateral
(values (case when type in ('middle soft','hard soft') then type else 'other' end)
) v(type)
group by v.type
order by (v.type = 'other') asc,-- put it last
count(*) desc;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。