按第一个字符分组 [英] group by first character
问题描述
我在Oracle SQL中有一个查询问题。
I have a problem with a query in Oracle SQL.
我在 first_name
列中有一个员工
表。我想根据 first_name
中的第一个字符对记录进行分组。
I have a first_name
column in an employees
table. I want to group my records according to the first character in first_name
.
例如,我有26条记录,一个带有 name ='Alice'
的名字,一个带有 name ='Bob'
的名字,依此类推名字的第一个字符。查询后,应该有26个组,每个组一个员工。
For example, I have 26 records, one with name = 'Alice'
, one with name = 'Bob'
, and so on down the alphabet for each name's first character. After the query, there should be 26 groups with one employee each.
我尝试了以下操作,但没有用:
I tried the following, but it's not working:
SELECT employee_id, (SUBSTR(first_name,1,1)) AS alpha FROM employees
GROUP BY alpha;
name_which_starts_from employees
A 10
B 2
C 4
D 9
E 3
G 3
H 3
I 2
J 16
K 7
L 6
M 6
N 4
O 1
P 6
R 3
S 13
T 4
V 2
W 3
推荐答案
您的查询是错误的,因为如果您想使之工作,则需要对EMPLOYEE_ID执行一些聚合功能。
Your query is wrong, since you would need to perform some aggregation function on EMPLOYEE_ID if you want that to work.
像:
select substr(first_name,1,1) as alpha, count(employee_id)
from employees
group by substr(first_name,1,1)
您到底想完成什么?
这篇关于按第一个字符分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!