Oracle:解码多个关键字段

时间:2014-10-29 07:34:04

标签: sql oracle decode

我有桌子

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

如果&#39; Date_pkp&#39;为空且&#39; date_pkp_delete&#39;是空的然后状态&#39; =非PKP

如果&#39; Date_pkp&#39;不为空且&#39; date_pkp_delete&#39;不是空的,那么&#39; status&#39; =非PKP

如果&#39; Date_pkp&#39;不为空且&#39; date_pkp_delete&#39;为空,然后是状态&#39; = PKP

如何使用decode进行查询?

从表中选择date_pkp,date_pkp_delete,Decode(...)状态

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

由于您必须检查多个条件,而不是编写complected decode,请使用case语句,如下所示:

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;