Reverse Update of 2 Columns - Tricky Tricky

  • Got a tricky one...  I need to update CustomerID and PageNumber with values in rows that precede it.  The first table is what I'm faced with... the second is what I need it to look like.  The biggest obsitcle I have is with updating only up to the point where there is another CustomerID and it's updating in reverse order.  Any help would be much appreciated!!!!

     

    Current Table

    ImportIDIDEmployeeIDYearCustomerIDPageNumber
    121975X11051997
    222067X10011997
    322097X10251997
    422108X12741997
    521905X10011997
    621974X11051997
    722140X11051997
    822146X10031997
    922174X10221997
    1121966X11051997
    1222065X10031997
    1322040X10221997
    1422091X10011997
    1522012X12251997
    1622078X11051997
    1722081X10121997
    1822149X10021997
    19Page 1
    20GTEMO28274
    2122074X10021997
    2222079X11201997
    2322114X10031997
    2422199X10221997
    2522220X11201997
    2722240X10011997
    2821965X10021997
    2922077X11051997
    3122093X15331997
    3222109X14881997
    3422148X13581997
    3521871X13581997
    3622161X13581997
    3721907X10051997
    3822150X14851997
    3922193X10221997
    4022216X10031997
    4122228X10221997
    42Page 2
    43GTEMO28275

     

    What the table should look like... I will be deleting the 2 rows with CustomerID and PageNumber after the update.

    ImportIDIDEmployeeIDYearCustomerIDPageNumber
    121975X11051997GTEMO28274Page 1
    222067X10011997GTEMO28274Page 1
    322097X10251997GTEMO28274Page 1
    422108X12741997GTEMO28274Page 1
    521905X10011997GTEMO28274Page 1
    621974X11051997GTEMO28274Page 1
    722140X11051997GTEMO28274Page 1
    822146X10031997GTEMO28274Page 1
    922174X10221997GTEMO28274Page 1
    1121966X11051997GTEMO28274Page 1
    1222065X10031997GTEMO28274Page 1
    1322040X10221997GTEMO28274Page 1
    1422091X10011997GTEMO28274Page 1
    1522012X12251997GTEMO28274Page 1
    1622078X11051997GTEMO28274Page 1
    1722081X10121997GTEMO28274Page 1
    1822149X10021997GTEMO28274Page 1
    19Page 1
    20GTEMO28274
    2122074X10021997GTEMO28275Page 2
    2222079X11201997GTEMO28275Page 2
    2322114X10031997GTEMO28275Page 2
    2422199X10221997GTEMO28275Page 2
    2522220X11201997GTEMO28275Page 2
    2722240X10011997GTEMO28275Page 2
    2821965X10021997GTEMO28275Page 2
    2922077X11051997GTEMO28275Page 2
    3122093X15331997GTEMO28275Page 2
    3222109X14881997GTEMO28275Page 2
    3422148X13581997GTEMO28275Page 2
    3521871X13581997GTEMO28275Page 2
    3622161X13581997GTEMO28275Page 2
    3721907X10051997GTEMO28275Page 2
    3822150X14851997GTEMO28275Page 2
    3922193X10221997GTEMO28275Page 2
    4022216X10031997GTEMO28275Page 2
    4122228X10221997GTEMO28275Page 2
    42Page 2
    43GTEMO28275

     

    Thanks, Rich!!!

  • This should work (but it's untested):

    UPDATE YourTable SET PageNumber=(
     SELECT ID FROM YourTable B WHERE B.ImportID=(
      SELECT MIN(ImportID) FROM YourTable C
      WHERE C.ImportID>A.ImportID 
      AND ID LIKE 'Page %'
    )) FROM YourTable A
    UPDATE YourTable SET CustomerID=(
     SELECT ID FROM YourTable B WHERE B.ImportID=(
      SELECT MIN(ImportID)+1 FROM YourTable C
      WHERE C.ImportID>A.ImportID 
      AND ID LIKE 'Page %'
    )) FROM YourTable A

    Razvan

  • Perfecto!  Very Nice!  I was heading in the direction of a cursor.  Thank you very much for you time on this!!!

    Rich

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply