【发布时间】:2021-10-06 11:42:12
【问题描述】:
我正在尝试在 SQLPAD 上解决以下问题。
编写查询以返回名字以“A”、“B”、“C”或其他开头的演员的数量。 结果的顺序无关紧要。 您需要返回 2 列: 第一列是基于名字的第一个字母的演员组,使用以下内容:'a_actors'、'b_actors'、'c_actors'、'other_actors'来表示他们的组。 第二列是名字与模式匹配的演员数量。
表:演员
col_name | col_type
-------------+--------------------------
actor_id | integer
first_name | text
last_name | text
示例结果
actor_category | count
----------------+-------
a_actors | 13
b_actors | 8
到目前为止,我已经尝试过:
select CONCAT(substring(lower(first_name), 2, 1), '_actors') as actor_category , count(*)
FROM actor
group by actor_category
不知道如何检查其他情况。
【问题讨论】:
-
将问题的关键部分链接到外部站点是不可接受的。请删除链接,然后完成提问。
-
并提供minimal reproducible example,即提供示例数据(作为 DDL+DML)您的查询、您想要的结果和您的实际结果。
-
考虑在名字的第一个字母上使用大小写表达式。不确定为什么要从名字的第二个字符开始子字符串?你会使用 dbfiddle 来测试你的查询吗?
标签: sql sql-server tsql case