2

我一直在尝试这样做几个小时,但我一直得到一个空结果。到目前为止,我通过运行查询得到了下表

select count(posts) from dbtable group by user

user | posts
_____________
a   |   3
b   |   7
c   |   2
d   |   1
e   |   1

如何在不使用 max()/greatest() 甚至 LIMIT 函数的情况下向用户显示最多的帖子。我运行上述查询的原始表有一个所有帖子的列表以及提交每个帖子的用户,我只是将它们分组。

4

2 回答 2

2
select * from 
(
select user,count(posts) cnt from dbtable 
group by user
) t1
left join 
(
select user,count(posts) cnt from dbtable 
group by user
) t2 on (t1.user<>t2.user) and (t1.cnt<t2.cnt)

where t2.cnt is null  

SQLFiddle 演示

于 2013-04-19T07:56:01.160 回答
0

这是另一个可行的解决方案,但对于更大的表来说非常慢。我用一小部分 48 个条目对其进行了测试,它足够快,但是在一个有 200.000 条记录的表上,它运行了几个小时而没有完成。:)

select *
from
(
    select distinct column
    from table t0
    join table t1 on t0.key = t1.key
    where t0.column > t1.column
    order by t0.column desc
)
where rownum <= 1
;
于 2013-04-19T12:39:37.050 回答