group by first character

前端 未结 7 1666
天涯浪人
天涯浪人 2020-12-03 04:16

I have a problem with a query in Oracle SQL.

I have a first_name column in an employees table. I want to group my records according to the

7条回答
  •  攒了一身酷
    2020-12-03 05:14

    You'll need to group by everything that is not an aggregate function, so you can't have employee_id in the SELECT projection. You also need to group by just the first character of the first_name. Something like this should work:

    SELECT  SUBSTR(first_name, 1, 1) AS alpha, COUNT(*) AS employee_count
    FROM    employees
    GROUP   BY SUBSTR(first_name, 1, 1);
    

    That would group by the first letter of the first name, and show the number of employees that fall into that group.

提交回复
热议问题