programing tip

ORA-00979 표현에 의한 그룹이 아닙니다

itbloger 2020. 6. 24. 07:47
반응형

ORA-00979 표현에 의한 그룹이 아닙니다


다음 쿼리로 ORA-00979를 받고 있습니다.

SELECT cr.review_sk, cr.cs_sk, cr.full_name,
tolist(to_char(cf.fact_date, 'mm/dd/yyyy')) "appt",
cs.cs_id, cr.tracking_number
from review cr, cs, fact cf
where cr.cs_sk = cs.cs_sk
and UPPER(cs.cs_id) like '%' || UPPER(i_cs_id) || '%'
and row_delete_date_time is null
and cr.review_sk = cf.review_wk (+)
and cr.fact_type_code (+) = 183050
GROUP BY cr.review_sk, cr.cs_sk, cf.fact_date, cr.tracking_number
ORDER BY cs.cs_id, cr.full_name;

동일한 쿼리에서 GROUP BY 및 ORDER BY 절이 모두있는 예제를 찾을 수 없습니다. 한 번에 하나씩 그룹에서 각 필드를 제거하려고 시도했지만 여전히 동일한 오류가 발생합니다.


당신은 모든 열을 삽입해야 SELECT에서 GROUP BY또는 (같은 단일 값으로 결과를 압축 그들에 기능 사용 MIN, MAX또는 SUM).

왜 이런 일이 발생하는지 이해하는 간단한 예 : 다음과 같은 데이터베이스가 있다고 상상해보십시오.

FOO BAR
0   A
0   B

그리고 당신은 실행 SELECT * FROM table GROUP BY foo합니다. 즉, 데이터베이스는 첫 번째 열과 함께 단일 행을 반환하여 결과 0를 충족시켜야 GROUP BY하지만 이제 두 가지 값 중에서 bar선택할 수 있습니다. 어떤 결과는 기대 - AB? 또는 GROUP BY? 의 계약을 위반하여 데이터베이스가 둘 이상의 행을 리턴해야 합니까?


그룹 함수 인수가 아닌 GROUP BY모든 SELECT표현식을 절에 포함하십시오 .


너무 나쁜 오라클에는 이와 같은 제한이 있습니다. 물론 GROUP BY에없는 열의 결과는 임의적이지만 때로는 원할 수도 있습니다. 어리석은 Oracle, MySQL / MSSQL에서이 작업을 수행 할 수 있습니다.

그러나 Oracle에 대한 해결 방법이 있습니다.

다음 줄이 작동하지 않는 동안

SELECT unique_id_col, COUNT(1) AS cnt FROM yourTable GROUP BY col_A;

다음과 같은 0으로 Oracle을 속여서 열의 범위를 유지하지만 그룹별로 묶을 수는 없습니다 (숫자라고 가정하면 CONCAT 사용)

SELECT MAX(unique_id_col) AS unique_id_col, COUNT(1) AS cnt 
FROM yourTable GROUP BY col_A, (unique_id_col*0 + col_A);

다음을 수행해야합니다.

SELECT cr.review_sk, 
       cr.cs_sk, 
       cr.full_name,
       tolist(to_char(cf.fact_date, 'mm/dd/yyyy')) "appt",
       cs.cs_id, 
       cr.tracking_number
from review cr, cs, fact cf
where cr.cs_sk = cs.cs_sk
       and UPPER(cs.cs_id) like '%' || UPPER(i_cs_id) || '%'
       and row_delete_date_time is null
       and cr.review_sk = cf.review_wk (+)
       and cr.fact_type_code (+) = 183050
GROUP BY cr.review_sk, cr.cs_sk, cf.fact_date, cr.tracking_number, cs.cs_id, cr.full_name
ORDER BY cs.cs_id, cr.full_name;

당신이 포함의 덕택으로 모색 않는 경우 GROUP BY절에서 표현식 SELECT그룹 기능 (또는 집계 함수 또는 집계 열)과 같은 아니며, COUNT, AVG, MIN, MAX, SUM등 (에 집계 함수의 목록 )에 존재한다 GROUP BY절.

예 (올바른 방법) (여기서는 employee_id그룹 함수 (비 집계 열)가 없으므로에 표시 되어야합니다GROUP BY . 대조적으로 sum (salary)는 그룹 함수 (집계 열)이므로 GROUP BY에 나타나지 않아도됩니다. .

   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.

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.


In addition to the other answers, this error can result if there's an inconsistency in an order by clause. For instance:

select 
    substr(year_month, 1, 4)
    ,count(*) as tot
from
    schema.tbl
group by
    substr(year_month, 1, 4)
order by
    year_month

Same error also come when UPPER or LOWER keyword not used in both place in select expression and group by expression .

Wrong :-

select a , count(*) from my_table group by UPPER(a) .

Right :-

select UPPER(a) , count(*) from my_table group by UPPER(a) .

참고URL : https://stackoverflow.com/questions/1520608/ora-00979-not-a-group-by-expression

반응형