Oracle解码语句

时间:2015-01-21 20:32:00

标签: sql decode

我有一个为ln_prd_typ返回null的查询。我想用NA替换空值我该怎么做。 以下是我的查询

select '211' as "Asset_id",
(Select decode( lv12.ATTRIBUTE4, '', 'NA', lv12.ATTRIBUTE4 )
                    FROM XXFMSLS. XXFM_FAH_CUSTOM_LVL2_RULES lv12
                    WHERE lv12.LEVEL2_RULE_KEY = lvl1.LEVEL2_RULE_KEY  ) as "LN_PRD_TYP"
from xxfmsls.XXFM_FAH_CUSTOM_LVL1_RULES lvl1

1 个答案:

答案 0 :(得分:2)

如果要在子查询中执行此操作,请使用coalesce

select '211' as "Asset_id",
       (Select coalesce( lv12.ATTRIBUTE4, 'NA')
        FROM XXFMSLS. XXFM_FAH_CUSTOM_LVL2_RULES lv12
        WHERE lv12.LEVEL2_RULE_KEY = lvl1.LEVEL2_RULE_KEY
       ) as "LN_PRD_TYP"
from xxfmsls.XXFM_FAH_CUSTOM_LVL1_RULES lvl1;

如果问题是你仍然得到NULL值,那是因为子查询不匹配 - 所以内部select中的逻辑永远不会被执行。我建议改为left join

select '211' as "Asset_id",
       coalesce(lv12.ATTRIBUTE4, 'NA') as "LN_PRD_TYP"
from xxfmsls.XXFM_FAH_CUSTOM_LVL1_RULES lvl1 left join
     XXFMSLS.XXFM_FAH_CUSTOM_LVL2_RULES lv12
     on lv12.LEVEL2_RULE_KEY = lvl1.LEVEL2_RULE_KEY;