数据准备
CREATE TABLE test.test2 (
user_id string,
shop string )
ROW format delimited FIELDS TERMINATED BY '\t';
INSERT INTO TABLE test.test2 VALUES
( 'u1', 'a' ),
( 'u2', 'b' ),
( 'u1', 'b' ),
( 'u1', 'a' ),
( 'u3', 'c' ),
( 'u4', 'b' ),
( 'u1', 'a' ),
( 'u2', 'c' ),
( 'u5', 'b' ),
( 'u4', 'b' ),
( 'u6', 'c' ),
( 'u2', 'c' ),
( 'u1', 'b' ),
( 'u2', 'a' ),
( 'u2', 'a' ),
( 'u3', 'a' ),
( 'u5', 'a' ),
( 'u5', 'a' ),
( 'u5', 'a' );
需求
– 请统计:
–(1)每个店铺的UV(访客数)
– (2)每个店铺访问次数top3的访客信息。输出店铺名称、访客id、访问次数
输入
输出
--(1)每个店铺的UV(访客数)
select shop,count(distinct user_id) cnt
from test.test2
group by shop;
-- (2)每个店铺访问次数top3的访客信息。输出店铺名称、访客id、访问次数
with t1 as (
select shop,user_id,count(1) as cnt,
row_number() over (partition by shop order by shop , count(1) desc) as rn1
from test.test2
group by shop,user_id
)
select shop,user_id,cnt
from t1
where rn1<=3
order by shop,rn1;