2014-10-29 55 views
1

我有表甲骨文:解碼超過一個鍵字段

Date_pkp | date_pkp_delete | status 
<null> | <null>   | 
10.10.2000| 20.11.2009  | 
12.10.2005| <null>   | 

如果 'Date_pkp' IS NULL和 'date_pkp_delete' 爲null,則 '狀態'=非PKP

如果 'Date_pkp' 是NOT NULL和 'date_pkp_delete' 不爲空,則 '狀態'=非PKP

如果 'Date_pkp' 不是null, 'date_pkp_delete' 爲空,則 '狀態'= PKP

如何使用decode查詢?從表

SELECT date_pkp,date_pkp_delete,解碼(...)狀態

回答

1

這將是更容易被case表達要做到這一點:

SELECT date_pkp, 
     date_pkp_delete 
     CASE WHEN ((date_pkp IS NULL AND date_pkp_delete IS NULL) OR 
        (date_pkp IS NOT NULL AND date_pkp_delete IS NOT NULL)) 
      THEN 'Non PKP' 
      WHEN (date_pkp IS NOT NULL AND date_pkp_delete IS NULL) 
      THEN 'PKP' 
     END AS status 
FROM my_table 
+0

是的,工作正常。這對我來說是新的。謝謝 – flyingbird013 2014-10-29 07:59:21

1

既然你要檢查多個條件,而不是的書寫解碼,用例說明如下:

select DATE_PKP, DATE_PKP_DELETE, case when (DATE_PKP is null and DATE_PKP_DELETE is null) then 'Non PKP' 
             when (DATE_PKP is not null and DATE_PKP_DELETE is not null) then 'Non PKP' 
             when (DATE_PKP is not null and DATE_PKP_DELETE is null) then 'PKP status' 
            end STATUS 
from table; 
+0

是的,工作正常。這對我來說是新的。謝謝 – flyingbird013 2014-10-29 07:59:00