ORACLE SQL //如果是的话?

时间:2015-05-15 10:09:23

标签: oracle

我的SQL代码中出现了一个小问题:

LEFT JOIN
           (SELECT *
              FROM pilotage_usines_valeurs
             WHERE c_indicateur IS NOT NULL) v
              ON     v.id_usine = d.id_usine
                 AND v.annee = 2015
                 AND V.MOIS = D.MOIS
                 AND V.C_INDICATEUR = pi1.c_indicateur

有时pi1.c_indicateur is null。我如何测试它并写下行if pi1.c_indicateur is not null不要写它if pi1.c_indicateur is null

2 个答案:

答案 0 :(得分:1)

如果我理解你的意图,我建议你在ON子句中添加NOT NULL条件:

LEFT JOIN (SELECT *
             FROM PILOTAGE_USINES_VALEURS
             WHERE C_INDICATEUR IS NOT NULL) v
  ON v.ID_USINE = d.ID_USINE
     AND v.ANNEE = 2015
     AND v.MOIS = D.MOIS
     AND v.C_INDICATEUR = pi1.C_INDICATEUR
     AND pi1.C_INDICATEUR IS NOT NULL

分享并享受。

答案 1 :(得分:0)

NVL用于通过其他方式替换空值。语法类似于

NVL (FIELD_TO_BE_TESTED_FOR_NULL, VALUE_IF_NULL);

选项1:  当null为某个值时替换pi1.c_indicateur,以便V.C_INDICATEUR永远不会等于该值。

AND V.C_INDICATEUR = NVL(pi1.c_indicateur,'IMPOSSIBLE VALUE FOR V.C_INDICATEUR ');

选项2:   如果为null,则将V.C_INDICATEUR替换为某个值,并将pi1.c_indicate替换为其他值,以便在NULL为止时两者都不匹配。

AND (V.C_INDICATEUR,'ABC') = NVL(pi1.c_indicateur,'DEF');