Hi,I've run into a challenge that I hope someone can help with.There is a Customers table with a CustomerId and a RealCustomerId. .-------------------------------------. | CUSTOMERS | +--------+---------------+------------+ | CustId | CustName | RealCustId | +--------+---------------+------------+ | 1 | A | 1 | +--------+---------------+------------+ | 2 | B | 2 | +--------+---------------+------------+ | 3 | C | 5 | +--------+---------------+------------+ | 4 | D | 7 | +--------+---------------+------------+ | 5 | E | 5 | +--------+---------------+------------+ | 6 | F | 6 | +--------+---------------+------------+ | 7 | G | 8 | +--------+---------------+------------+ | 8 | H | 9 | +--------+---------------+------------+ | 9 | I | 9 | +--------+---------------+------------+ | 10 | J | 10 | +--------+---------------+------------+
Usually, the CustomerId is the same as the RealCustomerId. Sometimes, though, one Customer gets bought by another Customer and the purchasing CustomerId is inserted into the original Customer's RealCustomerId. The process isn't perfect, so it's possible for a chain of mergers to happen and for some RealCustomerIds to be out of date.Let's take CustId 4 as an example from the "Customers" table above.- CustId 4 was bought by CustId 7
- CustId 7 was bought by CustId 8
- CustId 8 was bought by CustId 9
The RealCustomerId was never updated for CustId 4 to show the newest owner - CustId 9 - and there is no easy way of tracking all the changes. I need to see this chain of events so that ALL the subsequent CustIds are linked to the original CustId, as shown in the "Anticipated Results" table below. .-------------------------------------. | ANTICIPATED RESULTs | +--------+---------------+------------+ | CustId | CustName | RealCustId | +--------+---------------+------------+ | 1 | A | 1 | +--------+---------------+------------+ | 2 | B | 2 | +--------+---------------+------------+ | 3 | C | 5 | +--------+---------------+------------+ | 4 | D | 7 | +--------+---------------+------------+ | 4 | D | 8 | +--------+---------------+------------+ | 4 | D | 9 | +--------+---------------+------------+ | 5 | E | 5 | +--------+---------------+------------+ | 6 | F | 6 | +--------+---------------+------------+ | 7 | G | 8 | +--------+---------------+------------+ | 7 | G | 9 | +--------+---------------+------------+ | 8 | H | 9 | +--------+---------------+------------+ | 9 | I | 9 | +--------+---------------+------------+ | 10 | J | 10 | +--------+---------------+------------+
CustId 4 would have three rows, each displaying one of the purchasing CustomerIds. In the real dataset, there could be any number of mergers and this chain could last over 3 CustIds or 20 CustIds.Any ideas?Thank you for your help. Here is the sample code:CREATE TABLE #Customers (CustId INT, CustName VARCHAR(1), RealCustId INT)INSERT INTO #Customers SELECT 1, 'A', 1 UNION ALLSELECT 2, 'B', 2 UNION ALLSELECT 3, 'C', 5 UNION ALLSELECT 4, 'D', 7 UNION ALLSELECT 5, 'E', 5 UNION ALLSELECT 6, 'F', 6 UNION ALLSELECT 7, 'G', 8 UNION ALLSELECT 8, 'H', 9 UNION ALLSELECT 9, 'I', 9 UNION ALLSELECT 10, 'J', 10 CREATE TABLE #AnticipatedResults(CustId INT,RealCustId INT) INSERT INTO #AnticipatedResultsSELECT 1,1 UNION ALLSELECT 2,2 UNION ALLSELECT 3,5 UNION ALLSELECT 4,7 UNION ALLSELECT 4,8 UNION ALLSELECT 4,9 UNION ALLSELECT 5,5 UNION ALLSELECT 6,6 UNION ALLSELECT 7,8 UNION ALLSELECT 7,9 UNION ALLSELECT 8,9 UNION ALLSELECT 9,9 UNION ALLSELECT 10,10