Variation of row numbering problem

  • I have a table with data that looks like this:

    COL1COL2CATEGORY
    11aA
    21bA
    32A
    43A
    51B
    62B
    73aB
    83bB

    and I need to transform it to:

    sequenceCOL2CATEGORY
    11aA
    21bA
    32A
    43A
    11B
    22B
    33aB
    43bB

    So the question is, with using cursors, how can I get a result set that includes sequential numbering that starts over with each new CATEGORY?  I can do it using a cursor based solution, but I hate to use cursors.

    Thanks,

    Gordon

    Gordon Pollokoff

    "Wile E. is my reality, Bugs Bunny is my goal" - Chuck Jones

  • I think this should get it. It isn't the most ideal approach, but it should work.

    update table set A.sequence = ( Select COUNT(*) from table B where B.category = A.category and B.sequence <= A.sequence) FROM table A

    If you can assume that categories will be sequence as in your example you could do a TOP 1 Sequence in the subselect instead of COUNT(*) and then run subtraction agaist the returned number and the current one. The updates may get in the way of that process, though. You could improve the performance and reliability of that process using a variable based temp table. If your categories are grouped together as in your example and you want the whole temp table thing explained just say so.

     

  • Aaron,

    Thanks for the reply.  I had already solved the problem though it was good to see that my solution was essentially the same as yours.

     

    Gordon Pollokoff

    "Wile E. is my reality, Bugs Bunny is my goal" - Chuck Jones

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

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