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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:18:19+00:00 2026-05-19T03:18:19+00:00

Hi my table looks some what like this. OldPart | NewPart | Demand ==========================

  • 0

Hi my table looks some what like this.

OldPart | NewPart | Demand
==========================
   C    |    D    |   3
   F    |         |   1
   A    |    B    |   5
   D    |    E    |   2
   E    |    F    |   0
   B    |    C    |   3
   Z    |         |   1
   M    |         |   7
   Y    |    Z    |   10

What I am trying to do is come up with a final table where the newest part’s demand is aggregated and the demand for parts before it are changed to 0.

So my resulting table will be something like this:

OldPart | NewPart | Demand
==========================
   C    |    D    |   0
   F    |         |   14
   A    |    B    |   0
   D    |    E    |   0
   E    |    F    |   0
   B    |    C    |   0
   Z    |         |   11
   M    |         |   7
   Y    |    Z    |   0

Thanks in advance.

  • 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-05-19T03:18:19+00:00Added an answer on May 19, 2026 at 3:18 am

    Your mileage may vary, CTEs are limited. Out-of-the-box they only allow 100 steps deep. (I think the same goes for stored procs)

    But … if you really want a recursive solution… something like this may work. (though it is not super efficient)

    Keep in mind, loops and the like may throw this in to a spin.

    with recurse as (
        select * from #t
        union all
        select t2.OldPart, t2.NewPart, t.Demand + t2.Demand as Demand from recurse t
        join #t t2 on t.NewPart = t2.OldPart
    )
    
    
    select * from (
    select OldPart, '' NewPart ,MAX(Demand) Demand from recurse
    where OldPart in (select OldPart from #t where NewPart = '')
    group by OldPart 
    ) X 
    union all 
    select distinct OldPart, NewPart, 0 
    from #t 
    where NewPart <> ''
    

    results are:

     
    F       14
    M       7
    Z       11
    A   B   0
    B   C   0
    C   D   0
    D   E   0
    E   F   0
    Y   Z   0
    

    Input is:

    create table #t (OldPart varchar, NewPart varchar, Demand int) 
    
    go 
    
    insert #t 
    select
       'C'   ,  'D'     , 3
      union all
    select
       'F'     ,   ''     ,  1
      union all
    select
       'A'     ,   'B'    ,  5
      union all
    select
       'D'     ,   'E'    ,  2
       union all
    select
       'E'     ,   'F'    ,  0
    
      union all
    select
       'B'    ,    'C'    ,  3
    
      union all
    select
       'Z'    ,    ''    ,   1
       union all
    select
       'M'    ,   ''     ,   7
      union all
    select
       'Y'    ,    'Z'   ,   10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a table structure that looks something like this(some details omitted): ColumnName
I have some data in a table that looks roughly like the following: table
I have some code to update a database table that looks like try {
My table looks like this (and I'm using MySQL): m_id | v_id | timestamp
My fact table looks like this yesterday a yesterday a yesterday a yesterday b
My source table looks like this Id StartDate 1 (null) 2 12/12/2009 3 10/10/2009
my table looks like this: +--------------------------+ | key | value-name | value | +-----+------------+-------+
I would like to display some datas of a member table. My table looks
My parent-child table looks like this: ID, ParentID, Name, Population 1, Null, Asia, 40
My table looks something like: ID | Type 01 | a 02 | c

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.