2012-08-09 101 views
0

我有2張桌子。一個表格包含所有父級帳戶,頂級層次結構。第二個表包含所有子級帳戶,這些帳戶可能會或可能不會與父級表中的父級帳戶匹配。我們的目標是創建一個查詢(SQL Server 2008,遞歸或非),以查找與父級匹配的所有子級帳戶,以及該子級本身可能是其他子級帳戶的父級。帳戶層次?父母賬戶有子女可以成爲父母的子女

簡而言之,一旦父母對孩子進行匹配,需要檢查以確保匹配中的孩子本身不是其他孩子帳戶的父母。我滿口,我希望它是有道理的。我也不知道層次結構的深度。

CREATE TABLE dbo.Parent_Accounts 
(Parent_Account_Key_Lookup  varchar(28)   NOT NULL, 
Account_Number   bigint  NOT NULL, 
Reference_Account_Number_1  bigint  NOT NULL, 
Reference_Account_Number_2  bigint  NOT NULL, 
OpenDate    int   NOT NULL, 
Status      char(1)    NOT NULL, 
Record_Created   smalldatetime NOT NULL, 
Active    bit   NOT NULL) 
GO 

CREATE TABLE dbo.Child_Accounts 
(Child_Account_Key_Lookup  varchar(28)   NOT NULL, 
Account_Number   bigint  NOT NULL, 
Reference_Account_Number_1  bigint  NOT NULL, 
Reference_Account_Number_2  bigint  NOT NULL, 
OpenDate    int   NOT NULL, 
Status      char(1)    NOT NULL, 
Record_Created   smalldatetime NOT NULL, 
Active    bit   NOT NULL) 
GO 

    WITH cte_Recursive 
AS (SELECT parent.Account_Number, 
     parent.Parent_Account_Key_Lookup, 
     parent.Reference_Account_Number_1, 
     parent.Reference_Account_Number_2, 
     parent.OpenDate, 
     parent.[Status], 
     parent.Record_Created, 
     parent.Active, 
     1 AS Hierarchy_Level 
    FROM dbo.Parent_Accounts parent 
    WHERE parent.Account_Number = 4498481055218674 
    UNION ALL 
    SELECT child.Account_Number, 
     child.Child_Account_Key_Lookup, 
     child.Reference_Account_Number_1, 
     child.Reference_Account_Number_2, 
     child.OpenDate, 
     child.[Status], 
     child.Record_Created, 
     child.Active, 
     cte.Hierarchy_Level + 1 
    FROM cte_Recursive cte 
    INNER JOIN dbo.Child_Accounts child 
     ON cte.Parent_Account_Key_Lookup = child.Child_Account_Key_Lookup) 

    --SELECT * FROM cte_Recursive 
      SELECT TOP 2 * FROM cte_Recursive 

INSERT INTO dbo.Parent_Accounts 
(Parent_Account_Key_Lookup, 
    Account_Number, 
    Reference_Account_Number_1, 
    Reference_Account_Number_2, 
    OpenDate, 
    [Status], 
    Record_Created, 
    Active) 
VALUES ('222248105521867419970702', 2222481055218674, 2222481060975466, 0, 19970702, 'U', '2010-11-18 12:46:00', 0) 

INSERT INTO dbo.Child_Accounts 
(Child_Account_Key_Lookup, 
    Account_Number, 
    Reference_Account_Number_1, 
    Reference_Account_Number_2, 
    OpenDate, 
    [Status], 
    Record_Created, 
    Active) 
VALUES ('222248105521867419970702', 2222481060975466, 2222481055218674, 2222481055218674, 19970702, 'L', '2010-11-19 08:33:00', 0), 
    ('222248106097546619970702', 2222481060982900, 2222481060989137, 2222481060975466, 19970702, 'U', '2010-11-19 16:54:00', 0), 
    ('222248106098290019970702', 2222481060989137, 0,    2222481060982900, 19970702, ' ', '2010-11-21 01:52:00', 1) 
+0

一旦你找到了孩子和家長之間的匹配你不能只檢查查看相應的子ID是否作爲父表中的父級出現?如果孩子可以成爲父母,那麼任何父母都應該出現在父母表中。 – EplusL 2012-08-09 19:01:00

+2

你能提供一個你想要結果看起來像什麼的例子嗎? – 2012-08-09 19:08:32

+0

我已經添加了數據來填充表格 – user1588107 2012-08-09 19:14:03

回答

0

該問題似乎無法指定如何確定孩子的孩子是否因缺乏兒童身份而結合在一起。如果CTE中的ON子句可以阻止孩子加入自己,那麼它將打破無限遞歸鏈。

樣本輸出和回答的意見應明確您是否僅需在ON條款或UNION條款中處理兒童的子女。

這至少停止遞歸通過限制層級,並提供未經塞滿他們的數據庫執行測試平臺:

declare @Parent_Accounts as table (
    Parent_Account_Key_Lookup varchar(28) NOT NULL, 
    Account_Number bigint NOT NULL, 
    Reference_Account_Number_1 bigint NOT NULL, 
    Reference_Account_Number_2 bigint NOT NULL, 
    OpenDate int NOT NULL, 
    Status char(1) NOT NULL, 
    Record_Created smalldatetime NOT NULL, 
    Active bit NOT NULL) 

declare @Child_Accounts as table (
    Child_Account_Key_Lookup varchar(28) NOT NULL, 
    Account_Number bigint NOT NULL, 
    Reference_Account_Number_1 bigint NOT NULL, 
    Reference_Account_Number_2 bigint NOT NULL, 
    OpenDate int NOT NULL, 
    Status char(1) NOT NULL, 
    Record_Created smalldatetime NOT NULL, 
    Active bit NOT NULL) 

INSERT INTO @Parent_Accounts 
    (Parent_Account_Key_Lookup, Account_Number, Reference_Account_Number_1, Reference_Account_Number_2, OpenDate, [Status], Record_Created, Active) VALUES 
    ('222248105521867419970702', 2222481055218674, 2222481060975466, 0, 19970702, 'U', '2010-11-18 12:46:00', 0) 

INSERT INTO @Child_Accounts 
    (Child_Account_Key_Lookup, Account_Number, Reference_Account_Number_1, Reference_Account_Number_2, OpenDate, [Status], Record_Created, Active) VALUES 
    ('222248105521867419970702', 2222481060975466, 2222481055218674, 2222481055218674, 19970702, 'L', '2010-11-19 08:33:00', 0), 
    ('222248106097546619970702', 2222481060982900, 2222481060989137, 2222481060975466, 19970702, 'U', '2010-11-19 16:54:00', 0), 
    ('222248106098290019970702', 2222481060989137, 0, 2222481060982900, 19970702, ' ', '2010-11-21 01:52:00', 1) 

; WITH cte_Recursive 
AS (SELECT parent.Account_Number, 
parent.Parent_Account_Key_Lookup, 
parent.Reference_Account_Number_1, 
parent.Reference_Account_Number_2, 
parent.OpenDate, 
parent.[Status], 
parent.Record_Created, 
parent.Active, 
1 AS Hierarchy_Level 
FROM @Parent_Accounts parent 
WHERE parent.Account_Number = 2222481055218674 
UNION ALL 
SELECT child.Account_Number, 
child.Child_Account_Key_Lookup, 
child.Reference_Account_Number_1, 
child.Reference_Account_Number_2, 
child.OpenDate, 
child.[Status], 
child.Record_Created, 
child.Active, 
cte.Hierarchy_Level + 1 
FROM cte_Recursive cte 
INNER JOIN @Child_Accounts child 
ON cte.Parent_Account_Key_Lookup = child.Child_Account_Key_Lookup and cte.Hierarchy_Level < 2) 
SELECT * FROM cte_Recursive