我有一个包含2列的表,user_id和connection_type。这两个字段不是唯一的。单个用户id可以多次出现,单个连接类型可以多次出现。如何针对每个user_id找到重复次数最多的connection_type?
我有一张表:
schema:
user_id -- INT
connection_type -- STRING
user_id connection_type
101 4g
102 3g
101 4g
101 2g
102 2g
101 4g
102 4g
101 4g
102 4g
101 4g
102 3g
102 3g
102 3g我需要上面的表格:
user_id mode
101 4g
102 3g发布于 2020-05-02 18:13:41
您可以使用窗口函数:
select user_id, connection_type as mode
from (select user_id, connection_type,
dense_rank() over (partition by user_id order by count(*) desc) as seq
from table t
group by user_id, connection_type
) t
where seq = 1;https://stackoverflow.com/questions/61557575
复制相似问题