Ошибка ora 00979

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_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;

I couldn’t find any examples that had both GROUP BY and ORDER BY clauses in the same query. I tried removing each field from the group by one at a time, but am still getting the same error.

Ben's user avatar

Ben

51.5k36 gold badges127 silver badges148 bronze badges

asked Oct 5, 2009 at 14:57

Theresa's user avatar

You must put all columns of the SELECT in the GROUP BY or use functions on them which compress the results to a single value (like MIN, MAX or SUM).

A simple example to understand why this happens: Imagine you have a database like this:

FOO BAR
0   A
0   B

and you run SELECT * FROM table GROUP BY foo. This means the database must return a single row as result with the first column 0 to fulfill the GROUP BY but there are now two values of bar to chose from. Which result would you expect — A or B? Or should the database return more than one row, violating the contract of GROUP BY?

answered Oct 5, 2009 at 15:02

Aaron Digulla's user avatar

Aaron DigullaAaron Digulla

320k108 gold badges596 silver badges816 bronze badges

8

Include in the GROUP BY clause all SELECT expressions that are not group function arguments.

Ram Sharma's user avatar

Ram Sharma

8,6567 gold badges43 silver badges56 bronze badges

answered Oct 5, 2009 at 15:01

Xaisoft's user avatar

XaisoftXaisoft

45.4k87 gold badges278 silver badges430 bronze badges

0

Too bad Oracle has limitations like these. Sure, the result for a column not in the GROUP BY would be random, but sometimes you want that. Silly Oracle, you can do this in MySQL/MSSQL.

BUT there is a work around for Oracle:

While the following line does not work

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

You can trick Oracle with some 0’s like the following, to keep your column in scope, but not group by it (assuming these are numbers, otherwise use 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);

Peter O.'s user avatar

Peter O.

32k14 gold badges81 silver badges95 bronze badges

answered Apr 26, 2011 at 18:26

Joseph Lust's user avatar

Joseph LustJoseph Lust

19.2k7 gold badges82 silver badges80 bronze badges

3

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.

BobRodes's user avatar

BobRodes

5,9182 gold badges24 silver badges26 bronze badges

answered Jul 11, 2017 at 6:49

fg78nc's user avatar

fg78ncfg78nc

4,6763 gold badges19 silver badges30 bronze badges

You should do the following:

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;

answered Apr 19, 2016 at 6:28

Pavel Zimogorov's user avatar

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) .

answered Mar 23, 2017 at 10:52

Vijay's user avatar

VijayVijay

4,6261 gold badge29 silver badges37 bronze badges

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

answered Feb 11, 2019 at 14:57

3pitt's user avatar

3pitt3pitt

87713 silver badges21 bronze badges

Adding an alternative solution for @Joseph Lust’s answer regarding random rows in each group. This is made possible in Oracle Database 19c or newer by using any_value keyword. Here is an example:

select customer_id,
       max ( order_datetime ),
       any_value ( store_id ),
       any_value ( order_status ),
       any_value ( order_id )
from   co.orders
group  by customer_id;

answered Oct 18, 2022 at 7:49

Yodi S.'s user avatar

Yodi S.Yodi S.

1061 silver badge5 bronze badges

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.

cнŝdk's user avatar

cнŝdk

31.2k7 gold badges56 silver badges77 bronze badges

answered Apr 7, 2016 at 7:26

Muhammad Nadeem's user avatar

Muhammad NadeemMuhammad Nadeem

3604 gold badges7 silver badges21 bronze badges

The answer of «Aaron Digulla» (the first at this time) inspired my solution for the same error code using Spring Boot 2 (JPA / Hibernate) and CriteriaQuery / CriteriaBuilder.

Make a List of selects, and add it to your criteriaQuery.multiselect()

List<Selection> selects = new ArrayList<>();
    selects.add(seccionRoot.get("id"));
    selects.add(synSeccionRoot.get("DDF"));
    selects.add(synSeccionRoot.get("TTYU"));
    selects.add(synSeccionRoot.get("4567"));
    selects.add(seccionRoot.get("22").get("223"));
    selects.add(tasaRoot.get("price"));
    selects.add(tasaRoot.get("chair"));

    cq.multiselect(selects.toArray(Selection[]::new));

Then you can cast the List to an Expression[]

cq.groupBy(selects.toArray(Expression[]::new));

answered Apr 27, 2022 at 14:46

ieselisra's user avatar

ieselisraieselisra

3874 silver badges16 bronze badges

ORA-00979

ORA-00979: это не GROUP BY выражение

Причина:

Предложение GROUP BY не содержит все выражения в SELECT предложении. SELECT выражения не включенные в групповую функцию (такие как AVG, COUNT, MAX, MIN, SUM, STDDEV или VARIANCE) должны просматриваться в GROUP BY предложении.

Действие:

Включите в предложение GROUP BY все SELECT выражения которые не являются аргументами групповой функции.

Why does this work

SELECT DISTINCT FIRSTNAME, LASTNAME
FROM books, CUSTOMERS, orders, orderitems
WHERE STATE IN('FL ', 'GA')
GROUP BY orders.order#, firstname, lastname
HAVING SUM(retail*quantity) > 80  

but when firstname, lastname is removed from group by it doesn’t?

ORA-00979: not a GROUP BY expression

Mark Hurd's user avatar

Mark Hurd

10.6k10 gold badges68 silver badges100 bronze badges

asked Apr 7, 2011 at 21:04

user490735's user avatar

1

As I can guess

First of all GROUP BY operation is performed and then DISTINCT. In GROUP BY clause you must indicate all non-aggregates . For example you are not permitted to do the following:

SELECT FIRST_NAME, LAST_NAME
FROM EMPLOYEES
GROUP BY HIRE_DATE

You should do it by this way:

SELECT FIRST_NAME, LAST_NAME
FROM EMPLOYEES
GROUP BY HIRE_DATE,FIRST_NAME, LAST_NAME

answered Apr 8, 2011 at 11:01

kupa's user avatar

kupakupa

1,8615 gold badges25 silver badges41 bronze badges

0

In that case where firstname, lastname are removed from the group by, you get that error because you’re SELECTing a column(s) that aren’t in the GROUP BY expression, or aren’t part of an aggregation/function (i.e. MIN, MAX, AVG, and others).

You could also eliminate the DISTINCT as well.

answered Apr 7, 2011 at 21:15

p.campbell's user avatar

p.campbellp.campbell

98.2k67 gold badges255 silver badges320 bronze badges

1

Have you tried to run a query and got the “ORA-00979: not a group by expression” error? Learn what it is and how to resolve the error in this article.

The ORA-00979 error happens when you have at least one column in your SELECT clause that is not in your GROUP BY expression when you are using an aggregate function.

Common aggregate functions include SUM, AVG, MIN, MAX, and COUNT. Any column or expression in your SELECT clause must also be listed in the GROUP BY clause.

Here’s an example of a query that will generate the error:

SELECT first_name, last_name, COUNT(*)
FROM student
GROUP BY first_name;

Result:

ORA-00979: not a GROUP BY expression

Why Do I Get The ORA-00979 Error?

This error happens because you’re using an aggregate function, and there is at least one column in the SELECT clause that is not in the GROUP BY clause.

Using the example query above:

SELECT first_name, last_name, COUNT(*)
FROM student
GROUP BY first_name;

Because I use an aggregate function (COUNT), I need to define all of the columns in a GROUP BY clause that are in the SELECT clause.

In this example, I have specified the first_name and last_name columns in the SELECT clause, but the last_name column is not in the GROUP BY clause.

Even if I have some fields in the GROUP BY clause, if I don’t specify all of the fields from the SELECT clause, I’ll still get an error.

For example:

SELECT first_name, last_name, address_state, COUNT(*)
FROM student
GROUP BY first_name, last_name

Result:

ORA-00979: not a GROUP BY expression

As you can see, this will still give me an error.

Why does Oracle give an error?

Because, if you don’t have a GROUP BY but you want to SELECT the column, Oracle doesn’t know what value to show when using this aggregate function. Should it show the first value? The last value? A random value?

How To Resolve the ORA-00979 Error

To resolve the ORA-00979: not a group by expression error, simply ensure that all of the GROUP BY columns match the SELECT clause.

You can do this by adding columns to the GROUP BY.

So, using the example above:

SELECT first_name, last_name, COUNT(*)
FROM student
GROUP BY first_name, last_name;

Result:

FIRST_NAME LAST_NAME COUNT(*)
John Smith 2
Mark Anderson 1
Michael Condor 1
Brendan Jefferson 1
Peter Stark 1
Sally Lincoln 1
Michelle Brumby 1
Amy Ford 1
Rose Minson 1
Tina Mitchell 1

Or, using the second example:

SELECT first_name, last_name, address_state, COUNT(*)
FROM student
GROUP BY address_state, first_name, last_name

Result:

FIRST_NAME LAST_NAME ADDRESS_STATE COUNT(*)
John Smith California 1
Mark Anderson Texas 1
Michael Condor Florida 1
Brendan Jefferson Florida 1
Peter Stark Illinois 1
Sally Lincoln California 1
Michelle Brumby Texas 1
Amy Ford Texas 1
Rose Minson California 1
Tina Mitchell Illinois 1
John Smith Texas 1

The columns don’t need to be in the same order to correct the error. They just need to be present.

One thing to remember with a GROUP BY clause, is if you give your columns aliases, you need to specify the original column or expression.

So, if you have a query like this:

SELECT first_name + ' ' + last_name AS full_name, COUNT(*)
FROM student
GROUP BY full_name;

Result:

ORA-00904: "FULL_NAME": invalid identifier

You get an error because you can’t refer to a column alias within the GROUP BY clause. You’ll need to use a query like this:

SELECT first_name + ' ' + last_name AS full_name, COUNT(*)
FROM student
GROUP BY first_name + ' ' last_name;
FULL_NAME COUNT(*)
John Smith 2
Mark Anderson 1
Michael Condor 1
Brendan Jefferson 1
Peter Stark 1
Sally Lincoln 1
Michelle Brumby 1
Amy Ford 1
Rose Minson 1
Tina Mitchell 1

Conclusion

So, in conclusion, the ORA-00979: not a group by expression error happens because the columns in the SELECT clause don’t match the columns in the GROUP BY clause. To resolve the error, make sure the columns match.

Lastly, if you enjoy the information and career advice I’ve been providing, sign up to my newsletter below to stay up-to-date on my articles. You’ll also receive a fantastic bonus. Thanks!

totn Oracle Error Messages


Learn the cause and how to resolve the ORA-00979 error message in Oracle.

Description

When you encounter an ORA-00979 error, the following error message will appear:

  • ORA-00979: not a GROUP BY expression

Resolution

The option(s) to resolve this Oracle error are:

Option #1

Rewrite the SELECT statement so that the column or expression listed in the SELECT list is also found in the GROUP BY clause.

Option #2

Remove the GROUP BY function (ie: MIN Function, MAX Function, SUM Function, COUNT Function) from the SELECT statement.

Option #3

Remove the expression from the SELECT list that was not in the GROUP BY clause.

For example, if you had tried to execute the following SELECT statement:

SELECT department, company, MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department;

You would receive the following error message:

Oracle PLSQL

You could correct this by including company in the GROUP BY clause as follows:

SELECT department, company, MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department, company;

Понравилась статья? Поделить с друзьями:
  • Ошибка ora 00936 missing expression oracle
  • Ошибка ora 00904 недопустимый идентификатор
  • Ошибка ora 00060
  • Ошибка ora 00054
  • Ошибка ora 00020