Oracle SQL:计算具有特定值的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20090822/
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-19 02:06:10  来源:igfitidea点击:

Oracle SQL: Count number of rows with specific value

sqloracle

提问by George Newton

I have a table like this:

我有一张这样的表:

ID | AGE 
--------
 1 | 27
 2 | 24
 1 | 25
 4 | 25
 1 | 25
 5 | 25
 5 | 25
...|...

As you can see, ID and AGE are not unique (there is a second ID that is unique, but that's irrelevant here).

如您所见,ID 和 AGE 不是唯一的(有第二个 ID 是唯一的,但这在这里无关紧要)。

I want to return all the distinct IDs with more than one specific value: for example, counting all the IDs with more than one AGE = 25; in the table above, this should yield [1, 5], (ID = 4 is not counted because 25 occurs only once; ID = 1 and ID = 5 IS included because 25 occurs more than once in AGE).

我想返回具有多个特定值的所有不同 ID:例如,计算具有多个 AGE = 25 的所有 ID;在上表中,这应该产生 [1, 5],(ID = 4 不计算在内,因为 25 只出现一次;ID = 1 和 ID = 5 被包括在内,因为 25 在 AGE 中出现不止一次)。

Basically what I want in invalid SQL:

基本上我想要的无效SQL:

SELECT DISTINCT id FROM table WHERE count(AGE = 25) > 1

Unfortunately, count(AGE = 25) is invalid. How would I write this out in SQL?

不幸的是,count(AGE = 25) 无效。我将如何在 SQL 中写出它?

回答by Mithrandir

Do you want something like this?

你想要这样的东西吗?

SELECT 
 ID, COUNT(ID)
FROM table
WHERE Age = 25
GROUP BY ID
HAVING COUNT(ID) > 1