Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Populate other column value based on previous row value using t-sql

$
0
0

Hi All,

I have one table with 6 columns, let say ID1, ID2,status, EnteredDate,NewValue, Old Value. Where ID1 is the primary key field

 ID1       ID2       status             EnteredDate        NewValue       Old Value
  1          XYZ       New              07/12/2012            ABC               null
  2          XYZ       Renewal        08/19/2012            DEF                null
  3          XYZ       Cancel           10/21/2012            GHI                null
  4          ZYX       New              09/15/2012            BDF               null
  5          ZYX       Cancel           10/21/2012            MNS              null
  6          MBS       New              05/29/2012            EXP               null
  7          SBX        New              05/29/2012            SKS               null
  8          SBX        Renewal        06/21/2012            QSR               SKS

Basically I need a sql query which should populate Output as below. Status=New will always have old date compared to Renewal and Cancel and also OldValue field will be null always for status=New

Output:

 ID1       ID2       status           EnteredDate        NewValue       Old Value     Row_Num(based on ID1,ID2,Entereddate)
  1          XYZ       New              07/12/2012            ABC               null                 1
  2          XYZ       Renewal        08/19/2012            DEF                ABC                2
  3          XYZ       Cancel           10/21/2012            GHI                DEF                 3
  4          ZYX       New              09/15/2012            BDF               null                   1
  5          ZYX       Cancel           10/21/2012            MNS               BDF                 2
  6          MBS       New              05/29/2012            EXP               null                  1
  7          SBX        New              05/29/2012            SKS               null                  1
  8          SBX        Renewal        06/21/2012            QSR               SKS                2

Thanks in Advance, its very urgent. Pls send me the query ASAP.

RH


sql


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>