Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed query to select direct and indirect customerID aliases
    primarykey
    data
    text
    <p>I need a query that will return all related alias id's from either column. Shown here are some alias customer ids, among thousands of other rows. If the input parameter to a query is id=7, I need a query that would return 5 rows (1,5,7,10,22). That is because they are all aliases of one-another. For example, 22 and 10 are indirect aliases of 7.</p> <pre><code>CustomerAlias -------------------------- AliasCuID AliasCuID2 -------------------------- 1 5 1 7 5 7 10 5 22 1 </code></pre> <p>Here is an excerpt from the customer table.</p> <pre><code>Customer ---------------------------------- CuID CuFirstName CuLastName ---------------------------------- 1 Mike Jones 2 Fred Smith 3 Jack Jackson 4 Emily Simpson 5 Mike Jones 6 Beth Smith 7 Mike jones 8 Jason Robard 9 Emilie Jiklonmie 10 Michael jones 11 Mark Lansby 12 Scotty Slash 13 Emilie Jiklonmy 22 mike jones </code></pre> <p>I've been able to come close, but I cannot seem to select the indirectly related aliases correctly. Given this query:</p> <pre><code>SELECT DISTINCT Customer.CuID, Customer.CuFirstName, Customer.CuLastName FROM Customer WHERE (Customer.CuID = 7) OR (Customer.CuID IN (SELECT AliasCuID2 FROM CustomerAlias AS CustomerAlias_2 WHERE (AliasCuID = 7))) OR (Customer.CuID IN (SELECT AliasCuID FROM CustomerAlias AS CustomerAlias_1 WHERE (AliasCuID2 = 7))) </code></pre> <p>Returns 3 out of 5 of the desired ids of course. This lacks the indirectly related aliased id of 10 and 22 in the result rows.</p> <pre><code>1 Mike Jones 5 Mike Jones 7 Mike jones </code></pre> <p><em>*</em> Based on suggestions below, I am trying a CTE hierarchical query.</p> <p>I have this now after following some suggestions. It works for some, as long as the records in the table reference enough immediate ids. But, if the query uses id=10, then it still comes up short, just by the nature of the data.</p> <pre><code>DECLARE @id INT SET @id = 10; DECLARE @tmp TABLE ( a1 INT, a2 INT, Lev INT ); WITH Results (AliasCuID, AliasCuID2, [Level]) AS ( SELECT AliasCuID, AliasCuID2, 0 as [Level] FROM CustomerAlias WHERE AliasCuID = @id OR AliasCuID2 = @id UNION ALL -- Recursive step SELECT a.AliasCuID, a.AliasCuID2, r.[Level] + 1 AS [Level] FROM CustomerAlias a INNER JOIN Results r ON a.AliasCuID = r.AliasCuID2 ) INSERT INTO @tmp SELECT * FROM Results; WITH Results3 (AliasCuID, AliasCuID2, [Level]) AS ( SELECT AliasCuID, AliasCuID2, 0 as [Level] FROM CustomerAlias WHERE AliasCuID = @id OR AliasCuID2 = @id UNION ALL -- Recursive step SELECT a.AliasCuID, a.AliasCuID2, r.[Level] + 1 AS [Level] FROM CustomerAlias a INNER JOIN Results3 r ON a.AliasCuID2 = r.AliasCuID ) INSERT INTO @tmp SELECT * FROM Results3; SELECT DISTINCT a1 AS id FROM @tmp UNION ALL SELECT DISTINCT a2 AS id FROM @tmp ORDER BY id </code></pre> <p>Note that this is a simplified the query to just give a list of related ids.</p> <pre><code>--- id --- 5 5 7 10 </code></pre> <p>But, it is still unable to pull in ids 1 and 22.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload