Search code examples
sqldatabaseoracleoracle-sqldeveloper

Not a Group By expression : Unclear on how to fix the issue


I am unsure of the main issue here and believe my code is correct but I do not know how to fix the issue provided being "ORA-00979: not a GROUP BY expression".

SELECT distinct customer_city, customer_state, COUNT(*)
FROM customers
WHERE customer_state not in ('TX', 'OR')
GROUP BY ROLLUP(customer_state);

Solution

  • "distinct" is inappropriate for a group expression; your "group by" clause needs to include all non-grouped columns. Try this:

    SELECT customer_city, customer_state, COUNT(*)
    FROM customers
    WHERE customer_state not in ('TX', 'OR')
    GROUP BY ROLLUP (customer_city, customer_state);