案例表达与ABS

时间:2017-12-15 14:41:02

标签: sql oracle

我有下表:我正在寻找firstDate和EndDate之间的日子,但是当EndDate = 09/09/9000使用sysdate时。

表C

FirstDate      EndDate        id
 12/28/15     09/09/9000       1

我的下面有效,但是有更好的方式来写这个吗?

SELECT DISTINCT 
 id,
 ABS(TO_DATE(C.FirstDate) - TO_DATE(C.EndDate)) AS Between_days

       FROM (SELECT id, FirstDate,
             CASE WHEN  TO_CHAR(C2.EndDate,'MM/DD/YYYY') = '09/09/9000' THEN TRUNC(SYSDATE)
               ELSE C2.EndDate
                 END AS EndDate,
                 C2.ID,                   
              FROM tableC C2 ) C

1 个答案:

答案 0 :(得分:1)

您可以在没有派生表的情况下编写它:

SELECT DISTINCT
   id,
   CASE WHEN TO_CHAR(EndDate,'MM/DD/YYYY') = '09/09/9000'
        THEN ABS(TO_DATE(FirstDate) - TRUNC(SYSDATE))
        ELSE ABS(TO_DATE(FirstDate) - TO_DATE(EndDate))
   END as Between_days
FROM tableC