简体   繁体   English

在同一SQL查询中使用ROUND,AVG和COUNT

[英]Using ROUND, AVG and COUNT in the same SQL query

I need to write a query where I need to first count the people working in a department, then calculate the average people working in a department and finally round it to only one decimal place. 我需要编写一个查询,首先要计算部门中的人员,然后计算部门中的平均人员,最后将其四舍五入到小数点后一位。 I tried so many different variations. 我尝试了许多不同的变化。

That's what I got so far although it's not the first one I tried but I always get the same error message. 那是我到目前为止所获得的,尽管这不是我尝试的第一个,但是我总是收到相同的错误消息。 (ORA-00979 - not a group by expression) (ORA-00979-不是按表达式分组)

SELECT department_id,
    ROUND(AVG(c.cnumber),1)
FROM employees c
 WHERE c.cnumber =
           (SELECT COUNT(c.employee_id)
                FROM employees c)
GROUP BY department_id;

I really don't know what do to at this point and would appreciate any help. 我现在真的不知道该怎么办,将不胜感激。

Employees Table: 员工表: 在此处输入图片说明

Try this (Oracle syntax) example from your description: 从您的描述中尝试以下(Oracle语法)示例:

with department_count as (
SELECT department_id, COUNT(c.employee_id) as employee_count
FROM employees c
group by department_id
)
SELECT department_id,
    ROUND(AVG(c.employee_count),1)
FROM department_count c
GROUP BY department_id;

But this query not make sense. 但是这个查询没有意义。 Count is integer, and count return one number for one department in this case AVG return the same value as count. Count是整数,在这种情况下,AVG返回与count相同的值,count返回一个部门的一个数字。

Maybe you have calculate number of employee and averange of salary on department? 也许您已经计算出部门的员工人数和薪资范围?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM