hive SQL只为每个组选择最大计数值

nr9pn0ug  于 4个月前  发布在  Hive
关注(0)|答案(1)|浏览(56)

我有一个这样的查询:

select
  id,
  url,
  count(*) as count
from hit
where ...
group by
  id,
  url

字符串
有唯一的ID,通常有多个URL分配给它,但只有一个这些URL是正确的(我假设它是一个与最大计数)。因此,一旦我有查询结果,我想选择只有最大计数值为每个ID,同时只保留相应的URL。有没有办法做到这一点?最大(计数)显然不工作。

shyt4zoc

shyt4zoc1#

这个怎么样?我不完全确定它是否应该给予你所期望的,因为你没有给予任何样本数据。一点是:如果在一个id中有重复的最大计数,那么查询应该填充两个URL,因为你没有提到任何逻辑,其中一个要保留。

with make_cte as
(
    select id, url, count(*) as cnt
    from hit
    group by id, url
)
select id , url from
(
select id , url , cnt , max(cnt) over(partition by id) as max_cnt
from make_cte
) as a where cnt = max_cnt;

字符串

相关问题