Oracle错误:ORA-00979:不是GROUP BY表达式

时间:2018-11-23 02:04:15

标签: sql oracle

以下是我的第一个查询的结果(在Oracle中)。总计是每个部门的雇员总数(因此1-5号员工在1号部门,6-8号员工在2号部门等)

select a.name, (select count(*) from employee b where a.dname = b.dname group by b.dname) as totals
from employee a;

NAME                          TOTALS
------------------------------ ----------------
Employee 1                                    5
Employee 2                                    5
Employee 3                                    5
Employee 4                                    5
Employee 5                                    5
Employee 6                                    3
Employee 7                                    3
Employee 8                                    3
Employee 9                                    4
Employee 10                                   4
Employee 11                                   4
Employee 12                                   4

这是我的第二个查询,其中雇主的工作时间少于5个(请注意,“ taskhours”是与数据库中“ totals”不同的字段)

select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <5 
;

NAME
------------------------------
Employee 3

我现在要结合以上内容,即查询任务时间少于部门总雇员的雇员。我正在尝试查询

select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <(select count(*) from employee b where a.sname = b.sname group by b.sname)

但是我得到了错误:

ERROR at line 5:
ORA-00979: not a GROUP BY expression

有人可以帮助我找到正确的查询吗?

第一个查询的样本数据:

NAME                          DNAME
------------------------------ ------------------------------
Employee 1                            Dep1
Employee 2                            Dep1
Employee 3                            Dep1
Employee 4                            Dep1
Employee 5                            Dep1
Employee 6                            Dep2
Employee 7                            Dep2
Employee 8                            Dep2
Employee 9                            Dep3
Employee 10                           Dep3
Employee 11                           Dep3
Employee 12                           Dep3

2 个答案:

答案 0 :(得分:1)

您可以按如下方式简化第一个查询

将其用作cte块

with tot_count
  as (select dept_id,count(emp_id) as dept_cnt
        from employee
     group by dept_id)
   ,less_than_five
    as (select a.name
               ,sum(t.taskhours) as sum_taskhours
               ,max(a.dept_id) as dept_id_of_emp
          from job o, 
               employee a, 
               works w , 
               task t
        where w.name=a.name 
         and w.menuid=o.menuid 
         and w.worktype='office' 
         and t.fname = o.fname
       group by a.name 
        )
   select *
     from less_than_five a
     join tot_count b
       on a.dept_id_of_emp=b.dept_id
    where less_than_five.sum_taskhours<=b.dept_cnt

答案 1 :(得分:1)

我们可以通过将两个查询中的每个查询放入子查询中,然后将它们连接在一起来表达这一点:

SELECT
    e.name,
    e.dname,
    t1.emp_cnt,
    t2.hours_cnt
FROM employee e
LEFT JOIN
(
    SELECT dname, COUNT(*) AS emp_cnt
    FROM employee
    GROUP BY dname
) t1
    ON e.dname = t1.dname
LEFT JOIN
(
    SELECT a.name, SUM(t.taskhours) AS hours_cnt
    FROM works w
    INNER JOIN employee a
        ON w.name = a.name
    INNER JOIN job o
        ON w.menuid = o.menuid
    INNER JOIN task t
        ON t.fname = o.fname
    WHERE w.worktype = 'office'
    GROUP BY a.name
) t2
    ON e.name = t2.name
WHERE
    t1.emp_cnt < t2.hours_cnt;