sql - Date Aging Report + Cummulative -


i working on aging report based on last action date stored in table, given table below:

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 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 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 prod. req3        242     1/3/2013 14:56      completed 

i need generate aging table following columns requestno, usercode, receiveddate, lastactiondate, actiontype, aging (difference lastactiondate , receiveddate) cummulative.just table below

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 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 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 prod.  1       1 req3        242         12/28/2012 13:43    1/3/2013 14:56      completed           6       7 

are looking this?

with cte (   select *       (     select t.*, row_number() on (partition requestno order lastactiondate) rnum       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   cte c1 left join cte c2     on c1.requestno = c2.requestno    , c1.rnum = c2.rnum + 1  order requestno, lastactiondate 

output:

 | 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 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 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 prod. |     1 | |      req3 |      242 | december, 28 2012 13:43:00+0000 |  january, 03 2013 14:56:00+0000 |          completed |     6 | 

here sqlfiddle demo


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -