简体   繁体   English

SQL Server 2008:层次结构不严格的递归查询

[英]SQL Server 2008: Recursive query where hierarchy isn't strict

I'm dealing with a large multi-national corp.我正在与一家大型跨国公司打交道。 I have a table (oldtir) that shows ownership of subsidiaries.我有一张表(oldtir),显示子公司的所有权。 The fields for this problem are:此问题的字段是:

  • cID - PK for this table cID - 此表的 PK
  • dpm_sub - FK for the subsidiary company dpm_sub - 子公司的 FK
  • dpm_pco - FK for the parent company dpm_pco - 母公司的 FK
  • year - the year in which this is the relationship (because they change over time) year - 这是关系所在的年份(因为它们会随着时间而变化)

There are other fields, but not relevant to this problem.还有其他字段,但与此问题无关。 (Note that there are no records to specifically indicate the top-level companies, so we have to figure out which they are by having them not appear as subsidiaries.) (请注意,没有专门指明顶级公司的记录,因此我们必须通过让它们不显示为子公司来确定它们是什么。)

I've written the query below:我写了下面的查询:

with CompanyHierarchy([year], dpm_pco, dpm_sub, cID)
as (select distinct oldtir.[year], cast(' ' as nvarchar(5)) as dpm_pco, oldtir.dpm_pco as dpm_sub, cast(0 as float) as cID
    from oldtir 
    where oldtir.dpm_pco not in 
       (select dpm_sub from oldtir oldtir2 
          where oldtir.[year] = oldtir2.[year]
            and oldtir2.dpm_sub <> oldtir2.dpm_pco)
      and oldtir.[year] = 2011
    union all 
    select oldtir.[year], oldtir.dpm_pco, oldtir.dpm_sub, oldtir.cID
    from oldtir 
      join CompanyHierarchy
        on CompanyHierarchy.dpm_sub = oldtir.dpm_pco 
        and CompanyHierarchy.[year] = oldtir.[year]
      where oldtir.[year] = 2011 
             )

select distinct CompanyHierarchy.[Year], 
       CompanyHierarchy.[dpm_pco], 
       CompanyHierarchy.dpm_sub, 
   from CompanyHierarchy
   order by 1, 2, 3

It fails with msg 530: "The maximum recursion 100 has been exhausted before statement completion."它失败并显示 msg 530:“在语句完成之前,最大递归 100 已用尽。”

I believe the problem is that the relationships in the table aren't strictly hierarchical.我认为问题在于表中的关系不是严格分层的。 Specifically, one subsidiary can be owned by more than one company, and you can even have the situation where A owns B and part of C, and B also owns part of C.具体来说,一个子公司可以由多个公司拥有,甚至可以出现A拥有B和C的一部分,B也拥有C的一部分的情况。 (One of the other fields indicates percent of ownership.) (其他字段之一表示所有权百分比。)

For the time being, I've solved the problem by adding a field to track level, and arbitrarily stopping after a few levels.暂时我已经解决了这个问题,在track level中加了一个field,过了几个level就随意停了。 But this feels kludgy to me, since I can't be sure of the maximum number of levels.但这对我来说感觉很笨拙,因为我无法确定最大级别数。

Any ideas how to do this generically?任何想法如何一般地做到这一点?

Thanks, Tamar谢谢,添马舰

Thanks to the commenters.感谢评论者。 They made me go back and look more closely at the data.他们让我返回 go 并更仔细地查看数据。 There were, in fact, errors in the data, which led to infinite recursion.事实上,数据中存在错误,导致无限递归。 Fixed the data and the query worked just fine.修复了数据,查询工作得很好。

Add the OPTION statement and see if it makes a difference.添加 OPTION 语句,看看它是否有所作为。 This will increase the levels of recursion to 32K这会将递归级别增加到 32K

select distinct CompanyHierarchy.[Year], 
   CompanyHierarchy.[dpm_pco], 
   CompanyHierarchy.dpm_sub, 

from CompanyHierarchy order by 1, 2, 3 option (maxrecursion 0)从 CompanyHierarchy 按 1、2、3 选项排序(maxrecursion 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM