日期老化报告+累积

时间:2013-08-23 15:19:42

标签: sql

我目前正在根据存储在表格中的最后一个行动日期开展老龄化报告,如下表所示:

RequestNo   UserCode    LastActionDate      ActionType
REQ1        407     12/14/2012 9:47     Saved
REQ1        407     12/14/2012 9:48     Submitted
REQ1        407     12/14/2012 9:48     Approved
REQ1        203     12/17/2012 9:54     Reviewed
REQ1        242     12/18/2012 10:29    WF Setup in Dev.
REQ1        203     12/18/2012 15:14    Transport to prod.
REQ1        242     12/18/2012 15:16    Completed
REQ2        407     12/27/2012 10:36    Submitted
REQ2        456     12/27/2012 11:18    Approved
REQ2        407     12/27/2012 11:27    Approved
REQ2        203     12/27/2012 17:34    Reviewed
REQ2        242     12/28/2012 14:07    WF Setup in Dev.
REQ2        203     12/28/2012 14:11    Transport to prod.
REQ2        242     12/28/2012 21:27    Completed
REQ3        407     12/27/2012 11:32    Submitted
REQ3        456     12/27/2012 11:33    Approved
REQ3        407     12/27/2012 11:34    Approved
REQ3        203     12/27/2012 17:33    Reviewed
REQ3        242     12/27/2012 18:33    WF Setup in Dev.
REQ3        203     12/28/2012 13:43    Transport to prod.
REQ3        242     1/3/2013 14:56      Completed

我需要生成包含以下列的老化表 RequestNo,UserCode,ReceivedDate,LastActionDate,ActionType,Aging(差异LastActionDate和ReceivedDate)Cummulative.Just如下表

RequestNo   UserCode    ReceivedDate        LastActionDate      ActionType          Aging   Cummulative
REQ1        407         12/14/2012 9:47     12/14/2012 9:47     Saved               0       0
REQ1        407         12/14/2012 9:47     12/14/2012 9:48     Submitted           0       0
REQ1        407         12/14/2012 9:48     12/14/2012 9:48     Approved            0       0
REQ1        203         12/14/2012 9:48     12/17/2012 9:54     Reviewed            3       3
REQ1        242         12/17/2012 9:54     12/18/2012 10:29    WF Setup in Dev.    1       4
REQ1        203         12/18/2012 10:29    12/18/2012 15:14    Transport to prod.  0       4
REQ1        242         12/18/2012 15:14    12/18/2012 15:16    Completed           0       4
REQ2        407         12/27/2012 10:36    12/27/2012 10:36    Submitted           0       0
REQ2        456         12/27/2012 10:36    12/27/2012 11:18    Approved            0       0
REQ2        407         12/27/2012 11:18    12/27/2012 11:27    Approved            0       0
REQ2        203         12/27/2012 11:27    12/27/2012 17:34    Reviewed            0       0
REQ2        242         12/27/2012 17:34    12/28/2012 14:07    WF Setup in Dev.    1       1
REQ2        203         12/28/2012 14:07    12/28/2012 14:11    Transport to prod.  0       1
REQ2        242         12/28/2012 14:11    12/28/2012 21:27    Completed           0       1
REQ3        407         12/27/2012 11:32    12/27/2012 11:32    Submitted           0       0
REQ3        456         12/27/2012 11:32    12/27/2012 11:33    Approved            0       0
REQ3        407         12/27/2012 11:33    12/27/2012 11:34    Approved            0       0
REQ3        203         12/27/2012 11:34    12/27/2012 17:33    Reviewed            0       0
REQ3        242         12/27/2012 17:33    12/27/2012 18:33    WF Setup in Dev.    0       0
REQ3        203         12/27/2012 18:33    12/28/2012 13:43    Transport to prod.  1       1
REQ3        242         12/28/2012 13:43    1/3/2013 14:56      Completed           6       7

1 个答案:

答案 0 :(得分:0)

你在找这样的东西吗?

WITH cte AS
(
  SELECT *
    FROM
  (
    SELECT t.*, ROW_NUMBER() OVER (PARTITION BY RequestNo ORDER BY LastActionDate) rnum
      FROM Table1 t
  ) q
)
SELECT c1.RequestNo, c1.usercode, 
       COALESCE(c2.LastActionDate, c1.LastActionDate) ReceivedDate,
       c1.LastActionDate, c1.ActionType, 
       DATEDIFF(day, COALESCE(c2.LastActionDate, c1.LastActionDate), c1.LastActionDate) Aging
  FROM cte c1 LEFT JOIN cte c2
    ON c1.RequestNo = c2.RequestNo
   AND c1.rnum = c2.rnum + 1
 ORDER BY RequestNo, LastActionDate

输出:

| REQUESTNO | USERCODE |                    RECEIVEDDATE |                  LASTACTIONDATE |         ACTIONTYPE | AGING |
|-----------|----------|---------------------------------|---------------------------------|--------------------|-------|
|      REQ1 |      407 | December, 14 2012 09:47:00+0000 | December, 14 2012 09:47:00+0000 |              Saved |     0 |
|      REQ1 |      407 | December, 14 2012 09:47:00+0000 | December, 14 2012 09:48:00+0000 |          Submitted |     0 |
|      REQ1 |      407 | December, 14 2012 09:48:00+0000 | December, 14 2012 09:48:00+0000 |           Approved |     0 |
|      REQ1 |      203 | December, 14 2012 09:48:00+0000 | December, 17 2012 09:54:00+0000 |           Reviewed |     3 |
|      REQ1 |      242 | December, 17 2012 09:54:00+0000 | December, 18 2012 10:29:00+0000 |   WF Setup in Dev. |     1 |
|      REQ1 |      203 | December, 18 2012 10:29:00+0000 | December, 18 2012 15:14:00+0000 | Transport to prod. |     0 |
|      REQ1 |      242 | December, 18 2012 15:14:00+0000 | December, 18 2012 15:16:00+0000 |          Completed |     0 |
|      REQ2 |      407 | December, 27 2012 10:36:00+0000 | December, 27 2012 10:36:00+0000 |          Submitted |     0 |
|      REQ2 |      456 | December, 27 2012 10:36:00+0000 | December, 27 2012 11:18:00+0000 |           Approved |     0 |
|      REQ2 |      407 | December, 27 2012 11:18:00+0000 | December, 27 2012 11:27:00+0000 |           Approved |     0 |
|      REQ2 |      203 | December, 27 2012 11:27:00+0000 | December, 27 2012 17:34:00+0000 |           Reviewed |     0 |
|      REQ2 |      242 | December, 27 2012 17:34:00+0000 | December, 28 2012 14:07:00+0000 |   WF Setup in Dev. |     1 |
|      REQ2 |      203 | December, 28 2012 14:07:00+0000 | December, 28 2012 14:11:00+0000 | Transport to prod. |     0 |
|      REQ2 |      242 | December, 28 2012 14:11:00+0000 | December, 28 2012 21:27:00+0000 |          Completed |     0 |
|      REQ3 |      407 | December, 27 2012 11:32:00+0000 | December, 27 2012 11:32:00+0000 |          Submitted |     0 |
|      REQ3 |      456 | December, 27 2012 11:32:00+0000 | December, 27 2012 11:33:00+0000 |           Approved |     0 |
|      REQ3 |      407 | December, 27 2012 11:33:00+0000 | December, 27 2012 11:34:00+0000 |           Approved |     0 |
|      REQ3 |      203 | December, 27 2012 11:34:00+0000 | December, 27 2012 17:33:00+0000 |           Reviewed |     0 |
|      REQ3 |      242 | December, 27 2012 17:33:00+0000 | December, 27 2012 18:33:00+0000 |   WF Setup in Dev. |     0 |
|      REQ3 |      203 | December, 27 2012 18:33:00+0000 | December, 28 2012 13:43:00+0000 | Transport to prod. |     1 |
|      REQ3 |      242 | December, 28 2012 13:43:00+0000 |  January, 03 2013 14:56:00+0000 |          Completed |     6 |

这是 SQLFiddle 演示