问题是:对于每一天,列出已阅读最多邮件数的用户ID。
user_id msgID read_date
1 1 10
1 2 10
2 2 10
2 2 23
3 2 23
我相信日期是一个外部组,user_id是一个内部组,但是如何在sql中进行组嵌套?或者以某种方式避免这种情况?
答案 0 :(得分:1)
这是一个窗函数的任务:
select *
from
(
select user_id, read_date, count(*) as cnt,
rank()
over (partition by read_date -- each day
order by count(*) desc) as rnk -- maximum number
from tab
group by user_id, read_date
) dt
where rnk = 1
如果只想一个(随机)切换到ROW_NUMBER
答案 1 :(得分:0)
select user_id
from
(
select user_id,count(msgID)
from table
group by read_date
)
where rownum <= 1;