ORA-00937: not a single-group group function

user490735 picture user490735 · Apr 7, 2011 · Viewed 23k times · Source
SELECT MIN(retail)
FROM books
WHERE category = 'COMPUTER'

works fine, but when I include title in select like:

SELECT MIN(retail), title
FROM books
WHERE category = 'COMPUTER'

it doesn't. Why? How to make it work?

Answer

Mark Hurd picture Mark Hurd · Apr 8, 2011

Rhys's answer is correct, if that is what you mean, but you might have wanted the title(s) where retail=MIN(retail), and that wording suggests how to get that answer:

SELECT title, retail
FROM books
WHERE category = 'COMPUTER'
 AND retail = (SELECT MIN(retail) FROM books WHERE category = 'COMPUTER')

To reduce duplication you can use a WITH clause (if you're using a recent version of SQL):

;WITH ComputerBooks AS (
  SELECT title, retail
  FROM books
  WHERE category = 'COMPUTER')
SELECT title, retail
FROM ComputerBooks
WHERE retail = (SELECT MIN(retail) FROM ComputerBooks)

Sample I used to confirm syntax.