如何相加(计数)
我想添加一个计数(e.employee_id),然后按Manager_ID对其进行分组,因此我得到的结果是员工数量的
SELECT upper(concat(concat(concat(e2.First_name,' '),' '),e2.Last_Name)) "JEFE",
e.employee_id
FROM employees e join employees e2
on (e.manager_id = e2.employee_id);
结果是:
Name: Mike, Employee_Id: 101
Name: Mike, Employee_Id: 102
Name: Mike, Employee_Id: 103
Name: Mike, Employee_Id: 104
Name: Jason, Employee_Id: 201
Name: Jason, Employee_Id: 202
Name: Jason, Employee_Id: 203
Name: Jason, Employee_Id: 204
我希望结果是:
Name: Mike, Employee_Id: 4
Name: Jason, Employee_Id: 4
我尝试这样做:
SELECT upper(concat(concat(concat(e2.First_name,' '),' '),e2.Last_Name)) "JEFE",
count(e.employee_id)
FROM employees e join employees e2
on (e.manager_id = e2.employee_id)
group by e.manager_id;
但是SQL说:
ora-00979:不是通过表达式的组
00979。00000-“不是按表达式的组”
I would like to add a count(e.employee_id), and group it by manager_id so the result I get is the number of employees
SELECT upper(concat(concat(concat(e2.First_name,' '),' '),e2.Last_Name)) "JEFE",
e.employee_id
FROM employees e join employees e2
on (e.manager_id = e2.employee_id);
The result here is:
Name: Mike, Employee_Id: 101
Name: Mike, Employee_Id: 102
Name: Mike, Employee_Id: 103
Name: Mike, Employee_Id: 104
Name: Jason, Employee_Id: 201
Name: Jason, Employee_Id: 202
Name: Jason, Employee_Id: 203
Name: Jason, Employee_Id: 204
I want the result to be:
Name: Mike, Employee_Id: 4
Name: Jason, Employee_Id: 4
I tried doing this:
SELECT upper(concat(concat(concat(e2.First_name,' '),' '),e2.Last_Name)) "JEFE",
count(e.employee_id)
FROM employees e join employees e2
on (e.manager_id = e2.employee_id)
group by e.manager_id;
But the SQL said:
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过子句将名称添加到
组中,或将名称包装在聚合功能中。
您还可以使用层次查询,而不是(较慢的)自加入:
或:
对于示例数据:
这两个输出:
db<>fiddle
Either add the name to the
GROUP BY
clause or wrap the name in an aggregation function.You can also use a hierarchical query rather than a (slower) self-join:
or:
Which, for the sample data:
Both outputs:
db<>fiddle here