选择表达式时如何处理?

时间:2019-06-17 08:01:27

标签: sql oracle case-when

我试图用一个表达式做一个选择语句。
我收到错误

  

无效的sql语句

这是针对Oracle SQL:

SELECT DATUM_EINDE AANPAK,
       CASE WHEN DATUM_EINDE_AANPAK IS NULL THEN DATUM_EINDE_AANPAK
       ELSE MAX(DATUM_EINDE_AANPAK)
       END AS DATUM_EINDE_AANPAK
FROM T_CJIB_CVE;

3 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,我想这就是您想要的;对于ID = 2,原点为空,因此-从整个表格中获取最大原点。

SQL> with t_cjib_cve (id, datum_einde_aanpak) as
  2    (select 1, date '2019-06-17' from dual union all
  3     select 2, null              from dual union all
  4     select 3, date '2019-01-01' from dual
  5    )
  6  select datum_einde_aanpak,
  7         case when datum_einde_aanpak is null then
  8                     (select max(datum_einde_aanpak) from t_cjib_cve)
  9              else datum_einde_aanpak
 10         end result
 11  from t_cjib_cve;

DATUM_EIND RESULT
---------- ----------
2019-06-17 2019-06-17
           2019-06-17
2019-01-01 2019-01-01

SQL>

答案 1 :(得分:0)

像下面一样使用最大

SELECT DATUM_EINDE, AANPAK,
       max(CASE WHEN DATUM_EINDE_AANPAK IS NULL THEN DATUM_EINDE_AANPAK
       ELSE DATUM_EINDE_AANPAK END)
        AS DATUM_EINDE_AANPAK
FROM T_CJIB_CVE group by DATUM_EINDE, AANPAK

答案 2 :(得分:0)

在子查询中使用CASE检索最大日期

SELECT CASE WHEN datum_einde_aanpak IS NULL THEN null
  ELSE (SELECT MAX(datum_einde_aanpak) as max_date FROM t_cjib_cve) 
  END as datum_einde_aanpak
FROM t_cjib_cve a