我需要一个查询,它将从任一列返回所有相关的别名 id。此处显示的是一些别名客户 ID,以及其他数千行。如果查询的输入参数是 id=7,我需要一个返回 5 行(1、5、7、10、22)的查询。那是因为它们都是彼此的别名。例如,22 和 10 是 7 的间接别名。
CustomerAlias
--------------------------
AliasCuID AliasCuID2
--------------------------
1 5
1 7
5 7
10 5
22 1
这是客户表的摘录。
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
我已经能够接近,但我似乎无法正确选择间接相关的别名。鉴于此查询:
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)))
当然返回 5 个所需 id 中的 3 个。这在结果行中缺少 10 和 22 的间接相关别名 id。
1 Mike Jones
5 Mike Jones
7 Mike jones
*根据以下建议,我正在尝试 CTE 分层查询。
在遵循一些建议后,我现在有了这个。它适用于某些人,只要表中的记录引用足够的即时 ID。但是,如果查询使用 id=10,那么它仍然很短,只是由于数据的性质。
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
请注意,这是一个简化的查询,只提供相关 ID 的列表。
---
id
---
5
5
7
10
但是,它仍然无法拉入 id 1 和 22。