Select 整个 table oracle 中的单个最大值或最小值

Select a single maximum or minimum value in entire table of oracle

select table

不同列的最大值

例如,Table

 A  B  C
 -------
 1  2  3
 4  5  6
 7  8  9

结果会像

Max
9

怎么样:

select greatest(max(a), max(b), max(c))
  from your_table;

或者:

select max(x)
  from (select max(a) as x from your_table union all
        select max(b) from your_table union all
        select max(c) from your_table union all
  )

这是一个选项,它使用 GREATESTLEAST 函数,包含在 MAXMIN 聚合中:

SQL> with test (a, b, c) as
  2    (select 1, 2, 3 from dual union all
  3     select 4, 5, 6 from dual union all
  4     select 7, 8, 9 from dual
  5    )
  6  select max(greatest(a, b, c)) max_result,
  7         min(least(a, b, c)) min_result
  8  from test;

MAX_RESULT MIN_RESULT
---------- ----------
         9          1

SQL>

你可以试试这个

 select max(value) as Max from (
 select max(A) as value from example
 union 
 select max(B) as value from example 
 union
 select max(C) as value from example ) as tab;

假设值永远不会 NULL,我会简单地做:

select max(greatest(a, b, c))
from t;

您也可以这样表述:

select greatest(max(a), max(b), max(c))
from t;

此版本对 NULL 值更具弹性。它将与 NULLs 一起工作,除非列的 all 值为 NULL

它还将处理列中存在的 NULL 值。

WITH tempData (a, b, c) AS (SELECT NULL, 2, 3 FROM DUAL UNION ALL SELECT 4, 5, 6 FROM DUAL
UNION ALL SELECT 7, 8, NULL FROM DUAL)
SELECT GREATEST(MAX(a), MAX(b), MAX(c)) AS maxval, LEAST(MIN(a), MIN(b), MIN(c)) AS minval FROM tempData;

怎么样?

select greatest(NVL(c1, 0),NVL(c2, 0),NVL(c3, 0), NVL(c4, 0)) from T