Impala/SQL 如何将所有其他字段按语句放在一个组中?
Impala/SQL How can I put all the orther fields on a group by statement?
我有一个针对 100 个字段按 3 个字段分组的查询 table。如何在没有连接的情况下将另外 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);
谢谢!!
您可以改用相关子查询:
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
);
可以使用关联子查询
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
)
使用window函数:
select t.*
from (select t.*, max(d) over (partition by a, b, c) as max_d
from mytable t
where d = max_d;
我有一个针对 100 个字段按 3 个字段分组的查询 table。如何在没有连接的情况下将另外 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);
谢谢!!
您可以改用相关子查询:
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
);
可以使用关联子查询
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
)
使用window函数:
select t.*
from (select t.*, max(d) over (partition by a, b, c) as max_d
from mytable t
where d = max_d;