日期:2014-05-16  浏览次数:20782 次

求优化
有一个表有,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 dual
UNION 
SELECT 1 ,2 ,1 FROM dual
UNION 
SELECT 1,2 ,2 FROM dual
UNION 
SELECT 2 ,1 ,1 FROM dual
UNION 
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) c
FROM t;