2012-09-11 177 views
0

我有点混乱的数据集,我需要为特定的父帐户获取所有子帐户。递归SQL查询

的表如下

代码(PK),名称,ParentCode

没有外键

从我可以看到,如果一个帐户没有ParentCode是一个空字符串,或者它被设置为与代码相同。

我一直在尝试关注http://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx,但它似乎要返回所有数据,再加上我无法弄清楚如何才能让孩子只用一个帐户。

WITH DirectReports (ParentCus, Code, Name, Level) 
AS 
(
    SELECT ParentCode, Code, Name, 0 AS Level 
    FROM Customers 
     WHERE Code = ParentCode OR Code = '' 
    UNION ALL 

    SELECT c.ParentCode, c.Code, c.Name, level + 1 
    FROM Customers AS C 
    INNER JOIN DirectReports AS d 
     ON c.ParentCode = d.Code 
     where c.Code != d.Code 
) 
SELECT ParentCus, Code, Name, Level 
FROM DirectReports 

编辑 要清楚我需要能够通过我的功能的代码,并返回所有(递归),该账户子帐户。

终极密码 我不得不做出的答案稍有变化,但这里是最后的工作代码

declare @t table(code varchar(10), name varchar(10), parentcode varchar(10)) 
insert @t values(1, 'navn1', '1') 
insert @t values(2, 'navn2', '') 
insert @t values(3, 'navn3', 1) 
insert @t values(4, 'navn4', 3) 
insert @t values(5, 'navn5',4) 
insert @t values(6, 'navn6', 2) 
insert @t values(7, 'navn7', 3) 

declare @code varchar(10) -- or however code/parentcode is declared 
set @code = '1'   -- the parentcode you are trying to isolate 

;WITH DirectReports (ParentCus, Code, Name, Level) 
AS 
(
    SELECT ParentCode, Code, Name, 0 AS Level 
    FROM @t Customers 
    -- this picks the chosen code 
     WHERE Code = @code and (Code = ParentCode OR PARENTCode = '') 
    UNION ALL 
    SELECT c.ParentCode, c.Code, c.Name, level + 1 
    FROM 
     (select * from @t where code != parentcode) --had to do this to account for an infinate loop 
     as C 
    INNER JOIN DirectReports AS d 
     ON c.ParentCode = d.Code 

) 
SELECT ParentCus, Code, Name, Level 
FROM DirectReports 
-- level > 0: to ensure child accounts only 
where level > 0 
+0

你需要孩子通过parentId的?清楚地写下你需要的数据 – levi

回答

0

你需要指定你想要的代码。 这个例子是使用@t来代替你的表的客户

declare @t table(code varchar(10), name varchar(10), parentcode varchar(10)) 
insert @t values(1, 'navn1', '') 
insert @t values(2, 'navn2', '') 
insert @t values(3, 'navn3', 1) 

declare @code varchar(10) -- or however code/parentcode is declared 
set @code = '1'   -- the parentcode you are trying to isolate 

;WITH DirectReports (ParentCus, Code, Name, Level) 
AS 
(
    SELECT ParentCode, Code, Name, 0 AS Level 
    FROM @t Customers 
    -- this picks the chosen code 
     WHERE Code = @code and (Code = ParentCode OR PARENTCode = '') 
    UNION ALL 
    SELECT c.ParentCode, c.Code, c.Name, level + 1 
    FROM @t C 
    INNER JOIN DirectReports AS d 
     ON c.ParentCode = d.Code 
    -- added to prevent infinite loop, 
    -- I should point out that when this happens, you have bad data in your base 
    WHERE c.ParentCode <> c.Code 
) 
SELECT ParentCus, Code, Name, Level 
FROM DirectReports 
-- level > 0: to ensure child accounts only 
-- level < 100:to prevent infinite loops caused by circular references 
where level > 0 and level < 100 
+0

只需稍作改动(见编辑),但是这样做了。谢谢! – TheRealTy

+0

@tyrongower听起来像你有不好的数据,我写了另一种方式让你避免这种情况。 –

0
;With CTE as 
(
select ParentCode, Code, Name,row_number() over (order by (select 0)) as rn from Customers 
) 

,DirectReports as (ParentCus, Code, Name, Level) 
AS 
(
    SELECT rn,ParentCode, Code, Name, 0 AS Level 
    FROM CTE WHERE rn=1 and (Code = ParentCode OR Code = '') 
    UNION ALL 

    SELECT c.ParentCode, c.Code, c.Name, level + 1 
    FROM CTE AS C 
    INNER JOIN DirectReports AS d 
     ON c.ParentCode = d.Code and c.rn=d.rn+1 
     where c.Code != d.Code 
) 
SELECT ParentCus, Code, Name, Level 
FROM DirectReports