Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:24:02+00:00 2026-06-02T17:24:02+00:00

I have some legacy product data that has proven difficult to work with. The

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:24:04+00:00Added an answer on June 2, 2026 at 5:24 pm
    select OrderId,
           Sku,
           Type,
           Row_Id,
           (row_number() over(partition by Type order by Row_Id) - 1) %
            case Type
              when 'Single' then 1
              when 'Double' then 2
              when 'Triple' then 3
            end + 1
    from YourTable
    order by Row_Id
    

    SQL Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data (produced by a legacy application) that I know is invalid
I have some legacy data that was created on Windows XP. It contains absolute
I have some legacy python code that using pypar and mpich2 to transmit data
We have some legacy ASP.NET code that detects if a request is secure, and
I have some legacy code that was used to monitor my applications cpu,memory etc
I have some legacy C++ code that I am trying to understand a bit
I have some legacy code that contains this typedef: typedef enum simple_op_enum { #define
I have some legacy code that uses Interlocked.Equals to compare values. The values may
I have some legacy code that provides a list of the available COM ports
I have some legacy XSLT scripts that incorporate VBScript in them. They are run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.