sql - 计算包含 (SQL) 中给出的字母的记录
问题描述
我必须计算包含给定字母的记录,例如列 A 将包含包含“a”或“A”的记录计数,对于 E,它将包含包含“e”或“E”的记录计数。有没有办法只使用分组函数来做到这一点?
我可以通过使用子查询来做到这一点,但是在学习子查询之前,我们在课堂上有过这个任务,我不知道如何通过分组来做到这一点。
我想通过使用分组来实现以下代码的结果:
select
(select count(*) from table where lower(name) like '%a%') as a,
(select count(*) from table where lower(name) like '%e%') as e
from dual;
解决方案
您可以使用count + case
避免重复全表查询选择
select count(case when lower(name) like '%a%' then 1 end) as a
,count(case when lower(name) like '%e%' then 1 end) as e
from Table
推荐阅读
- docker - Gradle 6.3 Docker Palantir 插件:没有方法签名:build_8uxeoxw0v35xxabibl49jhru8.docker()
- continuous-integration - 如何同步 Gitlab、Github、Travis、Circle 等的 CI 配置?
- p5.js - 在 p5.js 中拉伸视频?
- c# - 如何在不更改 CSV 的情况下读取多行字段?
- php - 试图将电话号码设为链接
- javascript - 使用 PointsMaterial 和 CircleGeometry 创建相同的圆
- java - 如果可以的话,我可以尝试在设置器中捕获异常吗?我该如何正确地做到这一点?
- asp.net - 为什么 Visual Studio 2019 的 VB 编译器在我运行和编码时工作方式不同?
- assembly - eax为零时非法指令的解释
- json - 从 api fetch 调用映射数组的问题