ORA-00937:不是单组组功能

时间:2011-04-07 21:07:32

标签: sql min

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

工作正常,但当我在选择中包含标题时:

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

它没有。为什么?如何使它工作?

2 个答案:

答案 0 :(得分:8)

Rhys的答案是正确的,如果这是你的意思,但你可能想要title(s)retail=MIN(retail),并且该措辞建议如何得到答案:

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

为了减少重复,您可以使用WITH子句(如果您使用的是最新版本的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.

答案 1 :(得分:6)

MIN适用于一组记录,因此您需要告诉它您所指的记录组。

如果你的意思是每个标题,显示最低零售价,那么你需要:

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