在sql中使用GROUPBY进行查询
我有下表: ReviewID | Author_id | Posted time ------------------------------------- R1 | U1 | ... R2 | U2 R3 | U2 R9 | U7 R11 | U7 R12 | U7 R13 | U8 R14 | U10 s9商店的墙上有4个评论R11、R12、R13、R14。上表显示了哪个用户发布了哪个评论。从墙上的桌子上,我知道哪家商店的墙上写着所有的评论。因此,一旦我得到所有评论,我想找出发布评论最多的用户(即U7)在sql中使用GROUPBY进行查询,sql,oracle,Sql,Oracle,我有下表: ReviewID | Author_id | Posted time ------------------------------------- R1 | U1 | ... R2 | U2 R3 | U2 R9 | U7 R11 | U7 R12 | U7 R13 | U8 R14 | U10 s9商店的墙上有4个评论R11、R12、R13、R14。上表显示了哪个用户发布了哪个评论
上面的查询给了我U2,因为最大计数是2。(2是因为R11和R12之后的u7)有人能建议解决方法吗?就我所知,这是你想要的
select author_id from posted_reviews_tab group by author_id
having (count(author_id)) in (
select max(author_id) from posted_reviews_tab
where reviewid in (
select w.reviews.reviewid from wall_tab w where w.shopid = 'S9'
)
group by author_id
);
这会让你得到U7,但我不确定我是否理解你的要求,将结果限制在特定的商店
select author_id,
cnt
from (
select author_id,
count(*) cnt,
rank() over (order by count(*) desc) as rnk
from posted_reviews_tab
group by author_id
) t
where rnk = 1;
SQLFiddle示例:为什么使用
max(count(author\u id))
??对我来说没有任何意义。那么你到底想要什么英语呢?您是否只想要shopID s9中评论最多的作者?或者什么?是的,从shopID s9发布大多数评论的用户的authorID。count(author\u id)in(select max(author\u id)…
不起作用,因为max(author\u id)
返回的'U10'
无法与数值进行比较。这似乎是解决方案。不知道rank()。谢谢
select author_id,
cnt
from (
select author_id,
count(*) cnt,
rank() over (order by count(*) desc) as rnk
from posted_reviews_tab
group by author_id
) t
where rnk = 1;