ORA-00979 not a group by expression

后端 未结 8 1746
梦谈多话
梦谈多话 2020-11-21 04:33

I am getting ORA-00979 with the following query:

SELECT cr.review_sk, cr.cs_sk, cr.full_name,
tolist(to_char(cf.fact_date, \'mm/dd/yyyy\')) \"appt\",
cs.cs_i         


        
相关标签:
8条回答
  • 2020-11-21 05:04

    The group by is used to aggregate some data, depending on the aggregate function, and other than that you need to put column or columns to which you need the grouping.

    for example:

    select d.deptno, max(e.sal) 
    from emp e, dept d
    where e.deptno = d.deptno
    group by d.deptno;
    

    This will result in the departments maximum salary.

    Now if we omit the d.deptno from group by clause it will give the same error.

    0 讨论(0)
  • 2020-11-21 05:07

    If you do grouping by virtue of including GROUP BY clause, any expression in SELECT, which is not group function (or aggregate function or aggregated column) such as COUNT, AVG, MIN, MAX, SUM and so on (List of Aggregate functions) should be present in GROUP BY clause.

    Example (correct way) (here employee_id is not group function (non-aggregated column), so it must appear in GROUP BY. By contrast, sum(salary) is a group function (aggregated column), so it is not required to appear in the GROUP BYclause.

       SELECT employee_id, sum(salary) 
       FROM employees
       GROUP BY employee_id; 
    

    Example (wrong way) (here employee_id is not group function and it does not appear in GROUP BY clause, which will lead to the ORA-00979 Error .

       SELECT employee_id, sum(salary) 
       FROM employees;
    

    To correct you need to do one of the following :

    • Include all non-aggregated expressions listed in SELECT clause in the GROUP BY clause
    • Remove group (aggregate) function from SELECT clause.
    0 讨论(0)
提交回复
热议问题