找不到HR.EMPID时

时间:2013-07-26 20:19:50

标签: sql oracle

我的代码似乎忽略了我找不到HR.NAME的条目。我如何得到"未找到"在现场" SENT BY"?另外,如果它没有找到HR.NAME,请包含该数据。

SELECT DISTINCT MA.MOPID "MOP #"
   ,MA.MOPNOTIFICATIONSENT "Sent Status"
   ,MA.MOPNOTIFICATIONSENTBY "Employee ID"
   ,MA.MOPNOTIFICATIONSENDAT  "Date Sent"
   ,HR.NAME "SENT BY"

FROM MOPUSER.MOPACTIVITY MA ,HR@SECADMIN HR
WHERE TO_CHAR(MOPNOTIFICATIONSENDAT, 'YYYY-MM-DD') BETWEEN TO_CHAR(SYSDATE, 'YYYY-MM-DD') AND TO_CHAR(SYSDATE, 'YYYY-MM-DD')
AND HR.EMPID = SUBSTR(MA.MOPNOTIFICATIONSENTBY, 2, (LENGTH(MA.MOPNOTIFICATIONSENTBY) - 1))

1 个答案:

答案 0 :(得分:1)

尝试将COALESCE功能与left join

一起使用
SELECT DISTINCT MA.MOPID "MOP #"
   ,MA.MOPNOTIFICATIONSENT "Sent Status"
   ,MA.MOPNOTIFICATIONSENTBY "Employee ID"
   ,MA.MOPNOTIFICATIONSENDAT  "Date Sent"
   ,COALESCE(HR.NAME,'Not found') as "SENT BY"

FROM MOPUSER.MOPACTIVITY MA 
left join HR@SECADMIN HR on HR.EMPID = SUBSTR(MA.MOPNOTIFICATIONSENTBY, 2, (LENGTH(MA.MOPNOTIFICATIONSENTBY) - 1))
WHERE TO_CHAR(MOPNOTIFICATIONSENDAT, 'YYYY-MM-DD') BETWEEN TO_CHAR(SYSDATE, 'YYYY-MM-DD') AND TO_CHAR(SYSDATE, 'YYYY-MM-DD')