sql - 合并选择中的值
问题描述
我有下一部分查询:
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
解决方案
考虑一个对, 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
推荐阅读
- java - 在 IntelliJ 中使用 Boxable 的 NoClassDefFoundError
- c++ - C++:我可以更改向量类型吗?
- node.js - ReferenceError:初始化前无法访问“savedPost”
- visual-studio-code - VS Code 的责备窗口中的“用户”图像来自哪里?
- typescript - Vue.js 构建,在 buildtest 服务器上运行时未加载配置
- android - Apache poi 写入方法不会写入空白 Excel 工作表
- postgresql - 选择用户名字段而不在 groupby 中添加字段
- .net - 带有 AspNet Identity / OWIN 的 ASP.NET MVC5 应用程序过于频繁地调用 SQL Server 视图 INFORMATION_SCHEMA.COLUMNS
- html - 变形 SVG 与
SVG 或 CSS 过渡 - javascript - 模态没有出现在 laravel