题解 | #每篇文章同一时刻最大在看人数#
每篇文章同一时刻最大在看人数
http://www.nowcoder.com/practice/fe24c93008b84e9592b35faa15755e48
- 对于同样一个类型的字段,可以采用编码+联立的方式来求取同样一个时间在线的人数
- 对于同时进出的,要先统计进的,再统计出的
select artical_id, max(user_cnt) from (
select artical_id, t,
sum(diff) over(partition by artical_id order by t asc, diff desc) user_cnt
from (
select artical_id artical_id, in_time t, 1 diff from tb_user_log
where artical_id != 0
union all
select artical_id artical_id, out_time t, -1 diff from tb_user_log
where artical_id != 0 ) t1
) t2
group by 1
order by 2 desc