impala/sql如何将所有或其他字段放在groupby语句上?

58wvjzkj  于 2021-06-26  发布在  Impala
关注(0)|答案(3)|浏览(443)

我有一个根据100个字段表按3个字段分组的查询。如何将其他97个字段放入select中而不进行连接?
这是我的声明:

select a,b,c,max(d) as max_d
from mytable
group by a,b,c;

我知道下面的查询很有效,但它非常繁重:(

select mytable.* from
(
    select a,b,c,max(d) as max_d
from mytable
group by a,b,c
) uni
join mytable myt (uni.a=mytable.a AND uni.b=mytable.b AND uni.c=mytable.c AND uni.max_d=mytable.d);

谢谢!!

guz6ccqo

guz6ccqo1#

您可以改用相关子查询:

select mt.*
from mytable mt
where mt.d = (select max(mt1.d)
              from mytable mt1
              where mt1.a = mt.a and mt1.b = mt.b and mt1.c = mt.c
             );
z9smfwbn

z9smfwbn2#

可以使用共相关子查询

select t.* from mytable t
where t.d in ( select max(d) from mytable t1 
         where t1.a=t.a and t1.b=t.b and t1.c=t.c
              )
wlsrxk51

wlsrxk513#

使用窗口功能:

select t.*
from (select t.*, max(d) over (partition by a, b, c) as max_d
      from mytable t
where d = max_d;

相关问题