大数据SQL题
Posted MISAYAONE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据SQL题相关的知识,希望对你有一定的参考价值。
1:每个app下访问次数最多的5个用户
用户登陆日志
app, user_id, datetime
a1,user1,2020-01-01 00:00:00
a2,user2,2020-01-01 00:00:01
a3,user3,2020-01-01 00:00:02
a4,user4,2020-01-01 00:00:03
……
输出每个app下访问次数最多的5个用户
考察窗口函数:
select app,userid,rank from (
select app, userid, row_number() over (partition by app,userid order by c desc) as rank
from (
select app, userid, count(*) as c
from table group by app,userid
)
)
where rank <5
2:一定认识的用户的组合数
现有城市网吧访问数据,字段:网吧id,访客id(身份证号),上线时间,下线时间
规则1、如果有两个用户在一家网吧的前后上下线时间在10分钟以内,则两人可能认识
规则2、如果这两个用户在三家以上网吧出现【规则1】的情况,则两人一定认识
需求:
该城市上网用户中两人一定认识的组合数。
理解题目意思,梳理 join 条件,过滤符合条件的组合。
组合?t1.userid != t2.userid 这样在自己join自己的时候会出现两对。因此用的是t1.userid > t2.userid
select u1, u2, from (
select t1.userid as u1, t2.userid as u2, t1.store_id as store_id from
select * from table as t1
join
select * from table as t2
on t1.store_id = t2.store_id and t1.userid > t2.userid and
abs(t1.online_timestamp - t2.online_timestamp) < 600 and abs(t1.offline_timestamp - t2.offline_timestamp) < 600 )
group by u1,u2
having count (distinct store_id) > 3
3:每年收入前一万
现在有中国人从1980年到2020年每年每人的收入记录表A如下:
id,year,income
求每年收入前一万的人 窗口函数+ desc
select id,year,rank from (
select id,year,row_number() over (partition by year order by income desc) as rank
from table)
where rank < 10000
深度思考:每年13亿人,每个reducer处理的数据量很大,那么我们可以将id的前四位当作partition by 的参数,分组取topn,再合并。
select id, year, row_number() over (partition by year order by income desc) as rank_year from (
select id,year,rank from (
select id, year,
row_number() over (partition by year,substr(id,1,4) order by income desc) as rank
from table
)
where rank < 10000
)
where rank_year < 10000
4:最大连续登录天数
表字段,userid, install_time, active_time
为什么要给你注册时间,关键点就在于这个字段。登陆时间与固定时间间隔天数 和 登陆时间 rank 的差值
所有的登陆时间减去这个注册时间,就是注册与登录间隔的天数 diff_date,然后按照登陆时间大小给每条排序 rank,拿 diff_date - rank 应该是一个固定的值,求取这个固定值 group by count(*) 再取MAX。
2021.1.1 2021.1.2 diff_date: 1 rank:1
2021.1.1 2021.1.3 diff_date: 2 rank:2
2021.1.1 2021.1.4 diff_date: 3 rank:3
2021.1.1 2021.1.6 diff_date: 5 rank:4
select userid, max(c) from (
select userid, continue_date, count(1) as c from (
select userid, row_number() over (partition by userid order by active_time desc ) as
rank ,active_time - install_time as diff_time , diff_time-rank as continue_date
from (
select userid, unix_timestamp(install_time, 'YYYY-mm-dd HH:MM:SS') as
install_time,
unix_timestamp(active_time , 'YYYY-mm-dd HH:MM:SS') as active_time from table
)
group by userid, continue_date)
group by userid
5:当前连续登陆天数。
思路类似上题,依然是求得当天与固定天的差值,与当天时间的 rank 作差。
以上是关于大数据SQL题的主要内容,如果未能解决你的问题,请参考以下文章