SQL ORA-00937: 不是单组组函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5591154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:04:15  来源:igfitidea点击:

ORA-00937: not a single-group group function

sqlmin

提问by user490735

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?

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

回答by Mark Hurd

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:

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

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 WITHclause (if you're using a recent version of SQL):

为了减少重复,您可以使用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.

我用来确认语法的示例。

回答by Rhys Gibson

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

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

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