Not a group by expression oracle ошибка

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

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!

ORA-00979

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

Причина:

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

Действие:

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

oracle tutorial webinars

ORA-00979 is related to the GROUP BY clause. When a user encounters this error, the following message will be displayed:

ORA-00979: not a GROUP BY expression

In comparison to other Oracle errors, ORA-00979 is usually straightforward and can easily be solved by one of three methods.

The Problem

ORA-00979 occurs when the GROUP BY clause does not contain all the expressions in the SELECT clause. Any SELECT expression that is not included in the GROUP function must be listed in the GROUP BY clause. These are AVG, COUNT, MAX, MIN, SUM, STDDEV, and VARIANCE. You may have also tried to execute a SELECT statement that contains a GROUP BY clause.

The Solution

To correct this error, include all of the SELECT expressions in the GROUP BY clause. Make sure the expressions are not group function arguments. There are namely three methods for resolving this error.

  • Rewrite the SELECT statement so that the expression or column listed in the SELECT list is also in the GROUP BY clause.
  • You may choose to remove the GROUP BY function from the SELECT statement altogether.
  • Remove any expressions that do not belong in the GROUP BY clause from the SELECT list.

The first option in fixing the error would apply to the following example in which a user tries to execute a SELECT statement:

SELECT department, class, MAX(number) AS “Highest number of students”

FROM students

GROUP BY department;

In this example, the error may be corrected by including class in the GROUP BY clause. Class is included in both the SELECT and GROUP BY statements.

SELECT department, class, MAX(number) AS “Highest number of students”

FROM students

GROUP BY department, class;

Looking Forward

To avoid seeing ORA-00979, make sure that expressions in the SELECT list are also included in the GROUP BY clause. If the expression is also in the GROUP BY clause, you should not see the error. If you continue to see the error and have trouble resolving the issue, contact your database administrator. You may also consider contacting a licensed Oracle consultant. Before using their services, always make sure that they have proper credentials the level of experience needed to handle your Oracle needs.

ORA-00979: not a GROUP BY expression

Oracle PL/SQL error message: ORA-00979: not a GROUP BY expression.

Cause:

The GROUP BY clause does not contain all the expressions in the SELECT clause.
SELECT expressions that are not included in a group function(like AVG, COUNT, MAX, MIN, SUM, STDDEV or VARIANCE) must be listed in the GROUP BY clause.

Solution:

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

Example:

select p.PHONE_ID, p.PHONE_NAME model, pb.NAME brand, sum(po.AMOUNT)
from phones p, phone_brands pb, phone_orders po
where p.BRAND_ID=pb.BRAND_ID
and p.PHONE_ID=po.PHONE_ID
group by p.PHONE_ID

Output:

ORA-00979: not a GROUP BY expression

Correct:

select p.PHONE_ID, p.PHONE_NAME model, pb.NAME brand, sum(po.AMOUNT)
from phones p, phone_brands pb, phone_orders po
where p.BRAND_ID=pb.BRAND_ID
and p.PHONE_ID=po.PHONE_ID
group by p.PHONE_ID, p.PHONE_NAME, pb.NAME

Понравилась статья? Поделить с друзьями:
  • Norton power eraser ошибка
  • Normaliz dll easy anti cheat ошибка
  • Normalemail dotm ошибка
  • Normal mode focus 3 что означает ошибка
  • Normal dotm ошибка word как исправить