我有一个针对100个字段的表按3个字段分组的查询。如何在不加入联接的情况下将其他97个字段放入选择中?
这是我的声明:
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);
谢谢!
答案 0 :(得分:1)
您可以改为使用相关子查询:
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
);
答案 1 :(得分:0)
您可以使用相关的子查询
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
)
答案 2 :(得分:0)
使用窗口功能:
select t.*
from (select t.*, max(d) over (partition by a, b, c) as max_d
from mytable t
where d = max_d;