如何在 Hive 中按列分组以外的列应用 max 子句

how to apply max clause on column other than group by columns in Hive

我有一个包含如下数据的配置单元 table。

Table
---------------------
c1     c2      c3
a      1       7
a      2       6
a      3       3
a      3       1
a      3       2

我想编写一个查询以从 c3 列中获取值 2。逻辑是,对于列 c1 select max(c2) 然后在该 max(c2) 中找到 max(c3)

我写了类似

的查询
select c1, max(c3) from table1 
group by c1
having c2=max(c2)

但这没有用,因为 Hive 说我只能在 having 子句中使用属于 group by 的那些列。

请帮我解决这个问题。

注意:- 我需要一个查询。我可以在两个查询中写相同的内容

with your_data as (
select stack (5,
'a',1,7,
'a',2,6,
'a',3,3,
'a',3,1,
'a',3,2) as (c1,c2,c3)
)

select c1, max(c3) as max_c3
from
(
select c1,c2,c3,
       rank() over(partition by c1 order by c2 desc) rn --max(c2) marked 1
  from your_data
)s where rn=1 --filter records with max(c2)
group by c1

结果:

c1  max_c3  
a   3   

使用聚合函数:

create table val
(alpha varchar(10),id1  int,id2 int);

insert into val values ('a',3,3);
insert into val values ('a',3,1);
insert into val values ('a',3,2);

select alpha,id2 from
(
select alpha,max(id1) as id1,max(id2) as id2
from val group by alpha
)agg