I have some legacy product data that has proven difficult to work with. The products can be sold with 1, 2 or 3 parts, and the way the system was designed, parts 2 and 3 for an ordered product were simply subsequent rows after the first row for that product.
Here is some sample data….
----------------------------------------------------------
OrderId Sku Type Row_Id OtherColumns...
----------------------------------------------------------
123 001 Double 0 Other stuff..
123 001 Double 1 Other stuff..
123 001 Double 2 Other stuff..
123 001 Double 3 Other stuff..
123 002 Single 4 Other stuff..
123 003 Triple 5 Other stuff..
123 003 Triple 6 Other stuff..
123 003 Triple 7 Other stuff..
123 001 Double 8 Other stuff..
123 001 Double 9 Other stuff..
123 002 Single 10 Other stuff..
123 002 Single 11 Other stuff..
123 002 Single 12 Other stuff..
123 002 Single 13 Other stuff..
The old software (VB) deals with this by iterating over the rows and looking forward as it loops, getting the information it needs from the rows, and then skips them.
Fast forward 8 years…I have inherited this system in my new job and have rewritten the system from the ground up. The problem I’m having is getting that legacy data into my new format.
I’m looking for a way to select the same data, and partition it by the appropriate segment numbers. I’ve used RANK() OVER(PARTITION BY) with no success. I think I’m just not doing it right.
Ideally, I’d like to be able to generate a result set that looks like this: (NOTE the Segment column)
-------------------------------------------------------------------
OrderId Sku Type Row_Id Segment OtherColumns...
-------------------------------------------------------------------
123 001 Double 0 1 Other stuff..
123 001 Double 1 2 Other stuff..
123 001 Double 2 1 Other stuff..
123 001 Double 3 2 Other stuff..
123 002 Single 4 1 Other stuff..
123 003 Triple 5 1 Other stuff..
123 003 Triple 6 2 Other stuff..
123 003 Triple 7 3 Other stuff..
123 001 Double 8 1 Other stuff..
123 001 Double 9 2 Other stuff..
123 002 Single 10 1 Other stuff..
123 002 Single 11 1 Other stuff..
123 002 Single 12 1 Other stuff..
123 002 Single 13 1 Other stuff..
Ideally, I’d like to avoid cursors or loops. I’ll be using the query to migrate millions of records that are derived from multiple tables.
Thanks in advance for your help.
EDIT
I’ve updated the sample data to show that I do indeed have back to back groups that I need to isolate.
SQL Fiddle