0% found this document useful (0 votes)
128 views2 pages

Ora 00979 Not A Group by Expression Case Statement Issue

The document describes an issue where a query is returning an ORA-00979 error due to a CASE statement in the GROUP BY clause. The query is grouping on fields extracted from dates as well as the results of a CASE statement. Moving the CASE statement inside the SUM function is suggested to resolve the error by ensuring all non-aggregate fields in the GROUP BY are also in the SELECT.

Uploaded by

karthikmas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views2 pages

Ora 00979 Not A Group by Expression Case Statement Issue

The document describes an issue where a query is returning an ORA-00979 error due to a CASE statement in the GROUP BY clause. The query is grouping on fields extracted from dates as well as the results of a CASE statement. Moving the CASE statement inside the SUM function is suggested to resolve the error by ensuring all non-aggregate fields in the GROUP BY are also in the SELECT.

Uploaded by

karthikmas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

ORA-00979: not a GROUP BY expression (Case statement issue)

Ask Question
1

I am getting the ORA-00979 error message on the following query. I should preface
this by stating that I've read many of the discussions on this error on Stack
Overflow and still cannot find a solution. I'm also confident the error is coming
from the CASE statement, because when I completely removed the statement the query
has yielded results, and I also received results when I removed all of the CASE
statement except the 'SUM(Z.EQUITY_FOUR)' (that is, I'm also confident there is not
an issue with the database EQUITY_FOUR is pulling from).

SELECT
EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE) as Yr,
EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE) as Mth,
Q.CITY as City,
CASE WHEN pp.ISACTIVE = 'NO'
AND EXTRACT(YEAR FROM HDS.CANCELLATION_DATE) = EXTRACT(YEAR FROM
HDS.MORTGAGE_START_DATE)
AND EXTRACT(MONTH FROM HDS.CANCELLATION_DATE) = EXTRACT(MONTH FROM
HDS.MORTGAGE_START_DATE)
THEN SUM(Z.EQUITY_FOUR)
END as Cancelled

(From Statement)

(Where Statement)

GROUP BY EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE),


EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE),
Q.CITY,
CASE
WHEN pp.ISACTIVE = 'NO'
AND EXTRACT(YEAR FROM HDS.CANCELLATION_DATE) = EXTRACT(YEAR FROM
HDS.MORTGAGE_START_DATE)
AND EXTRACT(MONTH FROM HDS.CANCELLATION_DATE) = EXTRACT(MONTH FROM
HDS.MORTGAGE_START_DATE)
THEN SUM(Z.EQUITY_FOUR)
END

Concerning the CASE statement in the Group BY clause, while doing my research I
read this (ORA-00979: not a GROUP BY expression issue) post which contains two
responses stating to add the complete CASE statement to the GROUP BY expression and
which the author of the post claims worked for his query. I'm still getting the
above-specified error, however.

Thanks for any help!

I think you want the CASE inside the SUM():

SELECT EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE) as Yr,


EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE) as Mth,
Q.CITY as City,
SUM(CASE WHEN pp.ISACTIVE = 'NO' AND
EXTRACT(YEAR FROM HDS.CANCELLATION_DATE) = EXTRACT(YEAR FROM
HDS.MORTGAGE_START_DATE) AND
EXTRACT(MONTH FROM HDS.CANCELLATION_DATE) = EXTRACT(MONTH FROM
HDS.MORTGAGE_START_DATE)
THEN Z.EQUITY_FOUR
END) as Cancelled
(From Statement)
(Where Statement)
GROUP BY EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE),
EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE),
Q.CITY;

Gordon Linoff

book - Data Analysis Using SQL and Excel

You might also like