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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:34:22+00:00 2026-06-10T05:34:22+00:00

I have two tables, one is a Product Customer mapping and another is a

  • 0

I have two tables, one is a Product Customer mapping and another is a Product sharing table. Using SQL recursive CTE, given a product, I am trying to find all the products that are linked as a chain, mapped across the customers. In addition, if a Product is shared with another Product, I need to include it in the chain too. Hopefully, my example make more sense than the description

Product Customer Table
Product    Customer
  Milk     Illinois
  Milk     Michigan
  Butter   Michigan
  Cream    Wisconsin
  Honey    Wisconsin
  Cheese   Minnesota

Product Sharing Table
Product    SharedProduct
 Butter     Cream
 Cream      Cheese

For the above data, let’s say my input Product is Milk, then the result set should include all the products – Milk, Butter, Cream, Honey & Cheese. Here Butter-Cream & Cream-Cheese are linked through the Product Sharing table.

My current SQL looks like this but doesn’t really work more than one level deep.

WITH Product_CTE AS
(
  SELECT DISTINCT [Product] FROM ProductCustomer
  WHERE [Product] IN (SELECT DISTINCT p2.[Product]
  FROM ProductCustomer p1 INNER JOIN ProductCustomer p2
  ON p1.[Customer] = p2.[Customer] WHERE p1.[Product] = 'Milk')
  UNION ALL
  SELECT [SharedProduct] FROM ProductSharing b
  INNER JOIN Product_CTE p ON p.[Product] = b.[Product]
)
Select [Product] from Product_CTE
  • 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-10T05:34:23+00:00Added an answer on June 10, 2026 at 5:34 am

    A CTE has issues with multiple UNIONs. Although it may be possible, it wasn’t working for me.

    An alternative is to use a loop that stops when there are no more rows added to a working table:

    declare @ProductCustomers as Table ( Product VarChar(16), Customer VarChar(16) )
    insert into @ProductCustomers ( Product, Customer ) values
      ( 'Milk', 'Illinois' ),
      ( 'Milk', 'Michigan ' ),
      ( 'Butter', 'Michigan ' ),
      ( 'Cream', 'Wisconsin' ),
      ( 'Honey', 'Wisconsin' ),
      ( 'Cheese', 'Minnesota' )
    
    declare @ProductSharing as Table ( Product VarChar(16), SharedProduct VarChar(16) )
    insert into @ProductSharing ( Product, SharedProduct ) values
      ( 'Butter', 'Cream ' ),
      ( 'Cream', 'Cheese ' )
    
    declare @TargetProduct as VarChar(16) = 'Milk'
    
    declare @ProductChain as Table ( Product VarChar(16) )
    insert into @ProductChain ( Product ) values ( @TargetProduct )
    declare @NewRows as Int = 1
    
    while @NewRows > 0
      begin
      set @NewRows = 0
      -- Add products shared by the same customer.
      insert into @ProductChain
        select PCR.Product
          from @ProductCustomers as PCL inner join
            @ProductCustomers as PCR on
              -- Shared customer.
              PCR.Customer = PCL.Customer and
              -- Different product.
              PCR.Product <> PCL.Product
          where not exists ( select 42 from @ProductChain where Product = PCR.Product )
      set @NewRows = @NewRows + @@RowCount
      -- Add products linked through the product sharing table.
      insert into @ProductChain
        select PS.SharedProduct
          from @ProductSharing as PS inner join
            @ProductChain as PC on PC.Product = PS.Product
          where not exists ( select 42 from @ProductChain where Product = PS.SharedProduct )
      set @NewRows = @NewRows + @@RowCount 
      end
    
    select Product
      from @ProductChain
      order by Product
    

    Assumed herein is that the @ProductSharing table is unidirectional.

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

Sidebar

Related Questions

I have two tables One table connects client ID with product id TABLE A
i have two tables product product_id product_Name product_Price product_Description product_image category_id another table category
I have two tables, one with products and a foreign key for the customer.
I have two tables. One has some product details, the other holds various photos
I have two product tables Product1 and Product2. There is a one 2 one
I have two models. one is brand and another is product_detail. brands table has
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables : one table it matchs and the other is teams.
I have two tables connected with one to many relationship. Parent Table is a
I have two tables: one contains employees information and another is transactions (sales) information

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.