...其中 count(col) > 1

发布于 2024-08-12 18:22:22 字数 621 浏览 8 评论 0原文

我有一张这样的表:

+-----+-----+-------+
| id  | fk  | value |
+-----+-----+-------+
| 0   | 1   | peter |
| 1   | 1   | josh  |
| 3   | 2   | marc  |
| ... | ... | ...   |

我现在想获取具有多个值的所有条目。 预期的结果是:

+-----+-------+
| fk  | count |
+-----+-------+
| 1   | 2     |
| ... | ...   |

我试图这样实现:

select fk, count(value) from table where count(value) > 1;

但 Oracle 不喜欢它。

所以我尝试了这个……

select * from (
    select fk, count(value) as cnt from table
) where cnt > 1;

没有成功。

有什么想法吗?

I have a table like this:

+-----+-----+-------+
| id  | fk  | value |
+-----+-----+-------+
| 0   | 1   | peter |
| 1   | 1   | josh  |
| 3   | 2   | marc  |
| ... | ... | ...   |

I'd like now to get all entries which have more than one value.
The expected result would be:

+-----+-------+
| fk  | count |
+-----+-------+
| 1   | 2     |
| ... | ...   |

I tried to achieve that like this:

select fk, count(value) from table where count(value) > 1;

But Oracle didn't like it.

So I tried this...

select * from (
    select fk, count(value) as cnt from table
) where cnt > 1;

...with no success.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-08-19 18:22:22

使用having 子句来比较聚合。

此外,您需要根据要聚合的内容进行分组,查询才能正常工作。以下是一个开始,但由于您缺少 group by 子句,因此它仍然无法正常工作。你到底想算什么?

select fk, count(value) 
from table 
group by fk
having count(value) > 1;

Use the having clause for comparing aggregates.

Also, you need to group by what you're aggregating against for the query to work correctly. The following is a start, but since you're missing a group by clause still it won't quite work. What exactly are you trying to count?

select fk, count(value) 
from table 
group by fk
having count(value) > 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文