-3

I have a table named politics like this:

enter image description here

I want to group by WASDEPENDENT. But when I ran

select * from politics group by WASDEPENDENT

it raised an error:

ORA-00979: not a GROUP BY expression
00979. 00000 -  "not a GROUP BY expression"
*Cause:    
*Action:
Error at Line: 15 Column: 8

How can I solve this?

APC
  • 144,005
  • 19
  • 170
  • 281
dlwlrma
  • 808
  • 2
  • 10
  • 21
  • The general GROUP BY rule says: If a GROUP BY clause is specified, each column reference in the SELECT list must either identify a grouping column or be the argument of a set function! – jarlh Feb 22 '17 at 08:05
  • 1
    SELECT must contain columns that are specified in GROUP BY or columns with aggregate function on them. – gunjan maheshwari Feb 22 '17 at 08:06
  • You have to decide how to chose just one value from each column when a WASDEPENDENT value has several rows. – jarlh Feb 22 '17 at 08:07
  • Possible duplicate of [really simple SQL Not a GROUP BY expression - Oracle](http://stackoverflow.com/questions/13381451/really-simple-sql-not-a-group-by-expression-oracle) – Frank Schmitt Feb 28 '17 at 10:13

2 Answers2

1

GROUP BY is required for aggregating functions, such as SUM() or COUNT(). So a query like select * from politics group by WASDEPENDENT does not make sense.

Here is an example of the correct usage. This query counts how many records fall into each value of WASDEPENDENT:

select WASDEPENDENT, count(*) 
from politics 
group by WASDEPENDENT
order by WASDEPENDENT
APC
  • 144,005
  • 19
  • 170
  • 281
0

Cause

You tried to execute a SELECT statement that included a GROUP BY function (ie: MIN Function, MAX Function, SUM Function, COUNT Function) and an expression in the SELECT list that was not in the GROUP BY clause.

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