在 Oracle SQL 中查找列的最大值
Finding MAX of Column in Oracle SQL
我有以下 table 结构:
Table A (A1, ...) where A1 is PK
Table B (B1, B2, ...) where A.A1 = B.B1 and B2 is PK
Table C (C1, C2, C3, ...) where C.C1 = B.B2 and C1 is PK
鉴于提供了A.A1和C.C2,我需要获取C3的MAX()。以下显然不起作用:
select c.c3
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
where a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
and c.C3 = (
select max(c3)
from C
where c1 = c.C1)
和查询returns什么都没有。但是,当我尝试时:
select max(c.c3)
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
group by a.A1, c.C2
having a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
似乎 returns 是正确的值,但速度很慢。有没有办法让它与第一个查询一起工作(无分区)?
你可以试试WITH
子句。
您的查询可能是这样的:
;WITH C AS(
select c.c3
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
where a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
)
SELECT MAX(C3) FROM C
或者简单地说,
select max(cs)
from (
select c.c3 as cs
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
where a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
)
为什么不在 select
中使用 max(c3)
?
select max(c.c3)
from A a join
B b
on a.A1 = b.B1 join
C c
on b.B2 = c.C1
where a.A1 = '123' and c.C2 = date '2000-01-01';
我有以下 table 结构:
Table A (A1, ...) where A1 is PK
Table B (B1, B2, ...) where A.A1 = B.B1 and B2 is PK
Table C (C1, C2, C3, ...) where C.C1 = B.B2 and C1 is PK
鉴于提供了A.A1和C.C2,我需要获取C3的MAX()。以下显然不起作用:
select c.c3
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
where a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
and c.C3 = (
select max(c3)
from C
where c1 = c.C1)
和查询returns什么都没有。但是,当我尝试时:
select max(c.c3)
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
group by a.A1, c.C2
having a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
似乎 returns 是正确的值,但速度很慢。有没有办法让它与第一个查询一起工作(无分区)?
你可以试试WITH
子句。
您的查询可能是这样的:
;WITH C AS(
select c.c3
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
where a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
)
SELECT MAX(C3) FROM C
或者简单地说,
select max(cs)
from (
select c.c3 as cs
from A a join B b on a.A1 = b.B1
join C c on b.B2 = c.C1
where a.A1 = '123'
and c.C2 = to_date('01-01-2000', 'mm-dd-yyyy')
)
为什么不在 select
中使用 max(c3)
?
select max(c.c3)
from A a join
B b
on a.A1 = b.B1 join
C c
on b.B2 = c.C1
where a.A1 = '123' and c.C2 = date '2000-01-01';