在Oracle查询中加入条件

时间:2014-04-29 17:20:22

标签: oracle left-join inner-join

我有以下查询根据查询中提到的条件更新rec_tmprecord表的coulmn“tmp_activation_date”和最大actdate。

MERGE INTO rec_tmprecord
   USING (SELECT   rec.ID, MAX (act.actdate) AS tmpactivation_date
              FROM rec_tmprecord rec INNER JOIN tmp_asset asset
                   ON asset.serial = rec.serialtemp 
                   and upper (replace (asset.prodname, 'Ajink ')) = upper (replace (rec.prodnametemp, 'Ajink '))
                   and NVL(asset.release,'NF') = NVL(rec.tmprelease ,'NF')
                   and rec.serialtemp != 'aaa-aaaaaaaa'                   
                   LEFT JOIN tmp_acti_hist act
                   ON asset.tmp_id = act.tmp_row_id
             WHERE rec.cust_id = 234567
          GROUP BY rec.cust_id,asset.serial,rec.ID) new_values
   ON (rec_tmprecord.ID = new_values.ID)
   WHEN MATCHED THEN
      UPDATE
         SET tmp_activation_date = new_values.tmpactivation_date
      ;

但是,当我分析表“rec_tmprecord”的数据时,我发现“rec_tmprecord”表的“prodnametemp”列中存在一些空值或空值。但是,表“tmp_asset”的列“prodname” “不包含任何null或空值。 所以,我的连接条件将在“upper(replace(asset.prodname,'Ajink'))= upper(replace(rec.prodnametemp,'Ajink'))”条件失败,因此rec_tmprecord表的结果为“tmp_activation_date”使用空值或空值更新。

我的要求是,如果“prodnametemp”具有空值并且“prodname”包含某个值,那么“tmpactivation_date”将根据查询中提到的其他条件进行计算。 非常感谢Anyhelp。

1 个答案:

答案 0 :(得分:1)

这是修改后的版本。我按如下方式修改了连接:

upper (replace (asset.prodname, 'Ajink ')) = NVL(upper (replace (rec.prodnametemp, 'Ajink ')), upper (replace (asset.prodname, 'Ajink ')))

修改: 由于Alex Poole的建议,连接条件已被压缩。

基本上,如果为NULL,那么该条件将返回TRUE,因为那时asset.prodname = asset.prodname。

MERGE INTO rec_tmprecord
   USING (SELECT   rec.ID, MAX (act.actdate) AS tmpactivation_date
              FROM rec_tmprecord rec INNER JOIN tmp_asset asset
                   ON asset.serial = rec.serialtemp 
                   and upper (replace (asset.prodname, 'Ajink ')) = upper (replace (NVL (rec.prodnametemp, asset.prodname), 'Ajink '))
                   and NVL(asset.release,'NF') = NVL(rec.tmprelease ,'NF')
                   and rec.serialtemp != 'aaa-aaaaaaaa'                   
                   LEFT JOIN tmp_acti_hist act
                   ON asset.tmp_id = act.tmp_row_id
             WHERE rec.cust_id = 234567
          GROUP BY rec.cust_id,asset.serial,rec.ID) new_values
   ON (rec_tmprecord.ID = new_values.ID)
   WHEN MATCHED THEN
      UPDATE
         SET tmp_activation_date = new_values.tmpactivation_date
      ;