Oracle SQL - 计算列

时间:2018-02-02 22:43:55

标签: sql oracle date status

我需要一些帮助,根据项目状态,截止日期,完成日期和sysdate创建名为“Health_Stat”的计算列。我使用的是Oracle db。

+----------------+-----------------+---------------+-----------------------------------------------------------------+
| Project Status |    Due Date     | Health_Stat |                              Notes                              |
+----------------+-----------------+---------------+-----------------------------------------------------------------+
| Planning       | Before due date | Not Required  |                                                                 |
| Planning       | After due date  | Not Required  |                                                                 |
| Cancelled      | Before due date | Not Required  |                                                                 |
| Cancelled      | After due date  | Not Required  |                                                                 |
| In Process     | Before due date | On Track      | 2 Days before due date "Health_Stat" should change to "At Risk" |
| In Process     | After due date  | Overdue       |                                                                 |
| On Hold        | Before due date | On Track      | 2 Days before due date "Health_Stat" should change to "At Risk" |
| On Hold        | After due date  | Overdue       |                                                                 |
| Complete       | Before due date | On Track      |                                                                 |
| Complete       | After due date  | Overdue       |                                                                 |
+----------------+-----------------+---------------+-----------------------------------------------------------------+

下面是数据表:

+-----------+------------+------------+----------------+
| PROJECTID |   STATUS   |  DUE_DATE  | COMPLETED_DATE |
+-----------+------------+------------+----------------+
|     10135 | Complete   | 5/31/2016  | 5/31/2016      |
|     10138 | Complete   | 8/31/2016  |                |
|     10259 | Complete   | 4/30/2017  | 5/5/2017       |
|     10335 | Complete   | 7/31/2017  | 8/31/2017      |
|     10340 | On Hold    | 12/15/2017 | 11/30/2017     |
|     10356 | Complete   | 9/7/2017   | 9/7/2017       |
|     10404 | Cancelled  | 10/22/2017 | 11/6/2017      |
|     10433 | In Process | 11/8/2017  | 2/2/2018       |
|     10443 | Planning   | 11/22/2017 | 11/14/2017     |
|     10455 | Planning   | 12/15/2017 | 12/13/2017     |
+-----------+------------+------------+----------------+

这是我试过的查询:

SELECT projectid, 
       status, 
       due_date, 
       Trunc(completed_date) AS completed_date, 
       CASE 
         WHEN status IN ( 'In Process', 'On Hold', 'Complete' ) 
              AND due_date <= Trunc(completed_date) THEN 'On Track' 
         WHEN status IN ( 'In Process', 'On Hold', 'Complete' ) 
              AND due_date > Trunc(completed_date) THEN 'Overdue' 
         WHEN ( status IN ( 'Planning', 'Cancelled' ) ) THEN '' 
       END                   AS Health_Stat 
FROM   project_tracker 
ORDER  BY projectid 

以下是我得到的结果:

+-----------+------------+------------+----------------+--------------+
| PROJECTID |   STATUS   |  DUE_DATE  | COMPLETED_DATE | HEALTH_CHECK |
+-----------+------------+------------+----------------+--------------+
|     10135 | Complete   | 5/31/2016  | 5/31/2016      | On Track     |
|     10138 | Complete   | 8/31/2016  | -              | -            |
|     10259 | Complete   | 4/30/2017  | 5/5/2017       | On Track     |
|     10335 | Complete   | 7/31/2017  | 8/31/2017      | On Track     |
|     10340 | On Hold    | 12/15/2017 | 11/30/2017     | Overdue      |
|     10356 | Complete   | 9/7/2017   | 9/7/2017       | On Track     |
|     10404 | Cancelled  | 10/22/2017 | 11/6/2017      | -            |
|     10433 | In Process | 11/8/2017  | 2/2/2018       | On Track     |
|     10443 | Planning   | 11/22/2017 | 11/14/2017     | -            |
|     10455 | Planning   | 12/15/2017 | 12/13/2017     | -            |
+-----------+------------+------------+----------------+--------------+

如果在case语句中包含sysdate以及如何在截止日期前2天将health_stat更改为“On Track”,则会遇到问题。因为我必须将项目duedate与完成日期和sysdate进行比较。那就是我遇到麻烦的地方。感谢任何帮助。

谢谢,
Richa

以粗体字母更新正确的代码**: 感谢@MatBailie

SELECT projectid, 
       status, 
       due_date, 
       Trunc(completed_date) AS completed_date, 
       CASE 
         WHEN status IN ( 'In Process', 'On Hold', 'Complete' ) 
              AND due_date **>=** Trunc(completed_date) THEN 'On Track' 
         WHEN status IN ( 'In Process', 'On Hold', 'Complete' ) 
              AND due_date **<=** Trunc(completed_date) THEN 'Overdue' 
         WHEN ( status IN ( 'Planning', 'Cancelled' ) ) THEN '' 
         WHEN status IN ( 'In Process', 'On Hold' ) 
              AND due_date >= **SYSDATE** + 2 THEN 'On Track' 
         ELSE '?' 
       END                   AS Health_Stat 
FROM   project_tracker 
ORDER  BY projectid

1 个答案:

答案 0 :(得分:0)

SELECT projectid, 
       status, 
       due_date, 
       Trunc(completed_date) AS completed_date, 
       CASE 
         WHEN status IN ( 'In Process', 'On Hold', 'Complete' ) 
              AND due_date <= Trunc(completed_date) THEN 'On Track' 
         WHEN status IN ( 'In Process', 'On Hold', 'Complete' ) 
              AND due_date > Trunc(completed_date) THEN 'Overdue' 
         WHEN ( status IN ( 'Planning', 'Cancelled' ) ) THEN '' 
         WHEN status IN ( 'In Process', 'On Hold' )
              AND due_date >= SYS_DATE + 2 THEN 'On Track'
                                           ELSE '?'
       END                   AS Health_Stat 
FROM   project_tracker 
ORDER  BY projectid