...其中 count(col) > 1
我有一张这样的表:
+-----+-----+-------+
| 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
having
子句来比较聚合。此外,您需要根据要聚合的内容进行分组,查询才能正常工作。以下是一个开始,但由于您缺少 group by 子句,因此它仍然无法正常工作。你到底想算什么?
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?