在coalesce sql中没有选择空值

时间:2016-03-13 17:11:38

标签: sql oracle

我创建了一个查询来从位置和作业代码的映射登台表中获取值

 SELECT ORG_NAME,
        COALESCE(JOB_MAP.FUSION_HARMONIZED_CODE,JOB_CODE) JOB_CODE,
        JOB_SET_CODE,
        COALESCE(LOC_MAP.FUSION_HARMONIZED_CODE,LOCATION_CODE)LOCATION_CODE 
      FROM XX_POS POS_STAG,
        XX_MAP JOB_MAP,
        XX_MAP LOC_MAP
      WHERE JOB_MAP.SOURCE_CORE_HR_CODE(+)= POS_STAG.JOB_CODE
      AND JOB_MAP.TABLE_CODE(+)           ='JOB'
      AND POS_STAG.BUSINESS_UNIT_NAME     =JOB_MAP.business_unit
      AND LOC_MAP.TABLE_CODE(+)           ='LOC'
      AND LOC_MAP.SOURCE_CORE_HR_CODE(+)  = POS_STAG.LOCATION_CODE
      AND POS_STAG.BUSINESS_UNIT_NAME     =LOC_MAP.BUSINESS_UNIT;

但是这个查询没有获取location_code的值,该值在xx_pos表中为null。我必须改变我的状况吗?

1 个答案:

答案 0 :(得分:1)

我认为这是您想要的查询:

 SELECT ORG_NAME,
        COALESCE(JOB_MAP.FUSION_HARMONIZED_CODE, JOB_CODE) as JOB_CODE,
        JOB_SET_CODE,
        COALESCE(LOC_MAP.FUSION_HARMONIZED_CODE, LOCATION_CODE) as LOCATION_CODE 
FROM XX_POS POS_STAG LEFT JOIN
     XX_MAP JOB_MAP
     ON JOB_MAP.SOURCE_CORE_HR_CODE = POS_STAG.JOB_CODE AND
         JOB_MAP.TABLE_CODE = 'JOB' LEFT JOIN
     XX_MAP LOC_MAP
     ON LOC_MAP.SOURCE_CORE_HR_CODE = POS_STAG.LOCATION_CODE AND
        LOC_MAP.BUSINESS_UNIT = POS_STAG.BUSINESS_UNIT_NAME AND
        LOC_MAP.TABLE_CODE = 'LOC';