有一个表有,a,b,c3个值
现在要求,a对应的最大的b中的最大的c的那条数据
例如
a b c
1 1 3
1 2 1
1 2 2
2 1 1
2 2 1
结果要
a b c
1 2 2
2 2 1
我现在嵌套两个max,感觉比较慢,又没有性能好一点的方法?
------解决方案--------------------
以下方法试验一下性能。
select
b.a,
b.d,
max(b.c) e
from
(
select
a.a,
a.b,
a.c,
max(a.b) over(partition by a.a order by a.a) d
from
table_a a
) b
where b.b=b.d
group by
b.a,b.d
------解决方案--------------------
- SQL code
--S3为表名SELECT * FROM S3 T1 WHERE T1.B > = ALL (SELECT T2.B FROM S3 T2 WHERE T2.A = T1.A) AND T1.C >= ALL (SELECT T3.C FROM S3 T3 WHERE T3.A = T1.A AND T3.B = T1.B)
------解决方案--------------------
select * from (
select a,b,c ,row_number()over(partition by a order by b desc,c desc) num1 from xxxx
) where num1=1
------解决方案--------------------
- SQL code
WITH t AS(SELECT 1 a,1 b,3 c FROM dualUNION SELECT 1 ,2 ,1 FROM dualUNION SELECT 1,2 ,2 FROM dualUNION SELECT 2 ,1 ,1 FROM dualUNION SELECT 2 ,2 ,1 FROM dual)SELECT DISTINCT a ,Last_Value(b) over (PARTITION BY a ORDER BY b rows between unbounded preceding and unbounded following) b,Last_Value(c) over (PARTITION BY a ORDER BY b,c rows between unbounded preceding and unbounded following) cFROM t;