2013-07-26 65 views
1

我的代碼似乎忽略了我無法找到HR.NAME的條目。如何在「SENT BY」字段中找到「Not Found」?另外,如果它沒有找到HR.NAME,請包含該數據。當HR.EMPID未找到

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 ,[email protected] 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)) 
+0

我注意到,您可以訪問使用DBLINK的第一個字段是'HR.Name'所以我的猜測是,無論是場犯規與形同虛設。或者你沒有訪問權限。你可以訪問'HR @ SECADMIN'中的字段嗎? –

回答

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 [email protected] 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')