8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png
greatest最大值 Oracle
admin 2022-7-11

碰巧看到一篇文章提到了Oracle的greatest函数,相对应的有个least,以为这俩是个新的函数,但是看了下官方文档,其实在11g的《SQL Language Reference》,就提到了他们,是我孤陋寡闻了。

从函数的名称上,能猜到这两个函数分别求的是最大值和最小值。

greatest函数介绍,

GREATEST returns the greatest of a list of one or more expressions. Oracle Database uses the first expr to determine the return type. If the first expr is numeric, then Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type before the comparison, and returns that data type. If the first expr is not numeric, then each expr after the first is implicitly converted to the data type of the first expr before the comparison.

Oracle Database compares each expr using nonpadded comparison semantics. The comparison is binary by default and is linguistic if the NLS_COMP parameter is set to LINGUISTIC and the NLS_SORT parameter has a setting other than BINARY. Character comparison is based on the numerical codes of the characters in the database character set and is performed on whole strings treated as one sequence of bytes, rather than character by character. If the value returned by this function is character data, then its data type is VARCHAR2 if the first expr is a character data type and NVARCHAR2 if the first expr is a national character data type.

least函数介绍,

LEAST returns the least of a list of one or more expressions. Oracle Database uses the first expr to determine the return type. If the first expr is numeric, then Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type before the comparison, and returns that data type. If the first expr is not numeric, then each expr after the first is implicitly converted to the data type of the first expr before the comparison.

Oracle Database compares each expr using nonpadded comparison semantics. The comparison is binary by default and is linguistic if the NLS_COMP parameter is set to LINGUISTIC and the NLS_SORT parameter has a setting other than BINARY. Character comparison is based on the numerical codes of the characters in the database character set and is performed on whole strings treated as one sequence of bytes, rather than character by character. If the value returned by this function is character data, then its data type is VARCHAR2 if the first expr is a character data type and NVARCHAR2 if the first expr is a national character data type.

概括来讲,

1. greatest/least可以接收一个或多个字面值/字段列,返回其中的最大值/最小值。

2. greatest/least返回的数据类型,参照第一个参数的数据类型。

greatest作为例子,如果是字面值,直接返回最大值,

  1.  
    SQL> select greatest(123from dual;
  2.  
    GREATEST(1,2,3)
  3.  
    ---------------
  4.  
                  3
 

创建测试表,具备2个number类型,2个varchar2类型,

  1.  
    SQL> create table t_compare (id1 number, id2 number, name1 varchar2(1), name2 varchar2(1));
  2.  
    Table created.
  3.  
     
  4.  
     
  5.  
    SQL> insert into t_compare values(1, 2, 'a', 'c');
  6.  
    1 row created.
  7.  
     
  8.  
     
  9.  
    SQL> insert into t_compare values(23'A''D');
  10.  
    1 row created.
  11.  
     
  12.  
     
  13.  
    SQL> commit;
  14.  
    Commit complete.
 

当参数是两个number类型的,返回的是每行中数值最大的值,

  1.  
    SQL> select greatest(id1, id2from t_compare;
  2.  
    GREATEST(ID1,ID2)
  3.  
    -----------------
  4.  
    2
  5.  
    3
 

当参数是两个varchar2类型的,返回的是每行中字符最大的值,

  1.  
    SQL> select greatest(name1, name2from t_compare;
  2.  
    G
  3.  
    -
  4.  
    c
  5.  
    D
 

number和varchar2类型混合比较时,如果number在前,返回的是number类型,此时name1和name2是字母,不能转换成数字,因此报错,

  1.  
    SQL> select least(id1, id2, name1, name2) from t_compare;
  2.  
    select least(id1, id2, name1, name2) from t_compare
  3.  
    *
  4.  
    ERROR at line 1:
  5.  
    ORA-01722: invalid number
  6.  
     
 

如果name1和name2是能转换成数字的值,就可以执行,

  1.  
    SQL> insert into t_compare values(12'3''2', sysdate, sysdate);
  2.  
    1 row created.
  3.  
     
  4.  
     
  5.  
    SQL> select greatest(id1, id2, name1, name2from t_compare where name1='3';
  6.  
    GREATEST(ID1,ID2,NAME1,NAME2)
  7.  
    -----------------------------
  8.  
    3
 

如果varchar2在前,返回的是varchar2类型,此时id1和id2可以转换成varchar2,因此不会报错,

  1.  
    SQL> select greatest(name1, name2, id1, id2from t_compare;
  2.  
    GREATEST(NAME1,NAME2,ID1,ID2)
  3.  
    ----------------------------------------
  4.  
    c
  5.  
    D
 

除了数值、字符串类型,日期类型也能进行比较,

  1.  
    SQL> alter table t_compare add (t1 date, t2 date);
  2.  
    Table altered.
  3.  
     
  4.  
     
  5.  
    SQL> select * from t_compare;
  6.  
    ID1 ID2 N N T1 T2
  7.  
    ---------- ---------- - - ------------------ ------------------
  8.  
    1 2 a c 22-FEB-21 21-FEB-21
  9.  
    2 3 A D 22-FEB-21 21-FEB-21
  10.  
     
  11.  
     
  12.  
    SQL> select greatest(t1, t2) from t_compare;
  13.  
    GREATEST(T1,T2)
  14.  
    ------------------
  15.  
    22-FEB-21
  16.  
    22-FEB-21
 

如果date和number混合比较,提示两者不能互相转换,

  1.  
    SQL> select greatest(id1, id2, t1, t2) from t_compare;
  2.  
    select greatest(id1, id2, t1, t2) from t_compare
  3.  
    *
  4.  
    ERROR at line 1:
  5.  
    ORA-00932: inconsistent datatypes: expected NUMBER got DATE
  6.  
     
  7.  
     
  8.  
    SQL> select greatest(t1, t2, id1, id2) from t_compare;
  9.  
    select greatest(t1, t2, id1, id2) from t_compare
  10.  
    *
  11.  
    ERROR at line 1:
  12.  
    ORA-00932: inconsistent datatypes: expected DATE got NUMBER
  13.  
     
 

可以想象,Oracle可能存在其他和greatest/least相近的函数,看着很小,但在某些场景下,还是能起到一定的作用。个人理解,未必都得记住,当需要的时候,我们能进行有效的搜索,找到他们,知道怎么用,就可以了。

最后于 2022-8-24 被admin编辑 ,原因:
最新回复 (1)
    • 朕弟分享 | 专注小众,乐于分享!
      3
          
返回
发新帖 搜索 反馈 回顶部