Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Oracle SQL中查找列的最大值_Sql_Oracle_Aggregate Functions - Fatal编程技术网

在Oracle SQL中查找列的最大值

在Oracle SQL中查找列的最大值,sql,oracle,aggregate-functions,Sql,Oracle,Aggregate Functions,我有以下表格结构: 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

我有以下表格结构:

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)
查询不返回任何内容。但是,当我尝试时:

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')

它似乎返回了正确的值,但速度很慢。有没有办法让它处理第一个查询(无分区)?

您可以尝试使用
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')
)
为什么不在
选择中使用
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';

谢谢我在您的答案中添加了一个查询解决方案。我只是想知道为什么我发布的第一个查询不起作用。