首页 > 解决方案 > 计算包含 (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;

标签: sqloracle

解决方案


您可以使用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

推荐阅读