三种排序(排序开窗)
第几名/前几名/topN/top1
每个学生成绩第二高科目
select class,student from( select student,dense_rank() over(partition by studnet order by score) as rn from test )t1 where rn=2
一种百分排序---percent_rank()---算百分比超过了多少人
percent_rank()计算给定行的百分比排名。可以用来计算超过了百分之多少的人 (当前行的rank值-1)/(分组内的总行数-1) select name,subject,score, row_number() over(partition by subject order by score) as row_number, percent_rank() over(partition by subject order by score) as percent_rank from t_fraction
要求使用SQL统计每个用户累计1访问次数
select user_id,month_id,visit_cnt_1m, sum(visit_cnt_1n) over (partition by user_id order by month_id) as visit_cnt_td from (select id,month_id,sum(vicitCount) visit_cnt_1m from (select user_id,substr(visit_date,1,7) as month_id,visit_count from test )t1 group by month_id,visit_date)
求某个时间段内、某小时、每小时直播间最大在线人数
根据用户登录明细表(user_login_detail),求出平台同时在线最多的人数。
select max(s) cn from ( select user_id, event_ts, sum(cnt) over ( order by event_ts rows between unbounded preceding and current row ) s from ( select user_id, login_ts event_ts, 1 cnt from user_login_detail union all select user_id, logout_ts, -1 from user_login_detail ) t1 ) t2
完结,希望大家指正