0

我有下表:

评论ID | 作者_id | 发布时间
-------------------------------------
R1 | U1 | ...
R2 | U2
R3 | U2
R9 | U7
R11 | U7
R12 | U7
R13 | U8
R14 | U10

我的查询是这样的,我想得到 U7。我尝试了以下操作:

 select author_id 
 from posted_reviews_tab 
 group by author_id
 having (count(author_id)) in (
        select max(count(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
);    

shop s9 的墙上有 4 条评论 R11,R12,R13,R14。上表显示了哪个用户发布了哪个评论。从我的墙上的桌子上,我知道哪家商店的墙上写了哪些评论。所以,一旦我得到所有评论,我想找出发布最多评论的用户(即 U7) 上面的查询给了我 U2,因为最大计数是 2。(2 因为 u7 发布 R11 和 R12)任何人都可以建议解决方法?

4

2 回答 2

0

据我理解的问题,这就是你想要的..

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
  ); 
于 2013-06-12T19:29:27.477 回答
0

这会让您获得 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 示例:http ://sqlfiddle.com/#!4/52d62/1

于 2013-06-12T19:34:23.267 回答