ORA-00937: not a single-group group function

…衆ロ難τιáo~ 提交于 2019-12-01 01:47:40

问题


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?


回答1:


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.




回答2:


MIN applies to a group of records, so you need to tell it which group of records you mean.

If you mean for each title, show the minimum of retail, then you need:

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


来源:https://stackoverflow.com/questions/5591154/ora-00937-not-a-single-group-group-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!