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 7782123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:26:35+00:00 2026-06-01T19:26:35+00:00

I am working with data that is grouped by item number with counts. Each

  • 0

I am working with data that is grouped by item number with counts. Each record with a count > 2 needs to be blown out into individual records and compared at that level to a different set of data.

The data looks like this (It is stuck in this format. This is the only way the customer can send it.):

OwnerNumber ItemCode    ItemNumber  CountOfItems
1234    Item1   Item1-001   3
1234    Item1   Item1-002   1
1234    Item1   Item1-003   2
1234    Item2   Item2-001   1

And I need the data formatted like this (dynamically without hardcoding for value of CountOfItems):

OwnerNumber ItemCode    ItemNumber  
1234    Item1   Item1-001
1234    Item1   Item1-001
1234    Item1   Item1-001
1234    Item1   Item1-002
1234    Item1   Item1-003
1234    Item1   Item1-003
1234    Item2   Item2-001

For some reason I just can’t wrap my head around a clean way to do this (or any way).

  • 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-01T19:26:38+00:00Added an answer on June 1, 2026 at 7:26 pm

    You can manage with a Common Table Expression

    WITH CTE AS (
        SELECT OwnerNumber,ItemCode,ItemNumber,CountOfItems FROM table
    
        UNION ALL SELECT OwnerNumber,ItemCode,ItemNumber,CountOfItems-1
        FROM CTE
        WHERE CountOfItems >= 2
    )
    SELECT OwnerNumber,ItemCode,ItemNumber
    FROM CTE
    ORDER BY ItemNumber
    OPTION (MAXRECURSION 0);
    

    Edit:

    Added MAXRECURSION to handle situations where CountOfItems exceeds default max recursions as pointed out by Dev_etter

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

Sidebar

Related Questions

I am working on an application, that converts data from different companies into corresponding
I'm working with some data that's coming out of an SAP system. There's a
I am working with NSScanner to parse data that is both comma and line
We're working in a Dynamic Data project that will handle entities coming from two
I'm working with an existing EF data model that includes subclassed objects. That works
I have a large data set that I'm working with in excel. About 1000+
I'm working on an API that accepts data from remote clients, some of which
I'm working with a OPC Server control that stores data tags as variant types,
I have a working program in C++ that generates data for a Mandelbrot Set.
I have a data access class that took me a while to get working.

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.