简体   繁体   English

获取父子记录SQL Server 2008

[英]Get Parent Child records sql server 2008

I have COMPANY table like below 我有如下的COMPANY表

 CompanyID  CompanyName

 1           xyz
 2           xyz-c
 3           xyz-c1
 4           xyz-c2
 5           xyz-c-c
 6           xyz-c-c1
 7           xyz-c-c2
 8           xyz-c-c1-c
 9           xyz-c-c1-c1
10           xyz-c-c1-c2 

and i have a COMPANYMAPPING table like below 我有一个如下的COMPANYMAPPING表

 CompanyMapID   ParentCompanyID ChildCompanyID
  1                 1               2               
  2                 1               3               
  3                 1               4               
  4                 2               5               
  5                 2               6               
  6                 2               7               
  7                 6               8               
  8                 6               9               
  9                 6               10              
 10                 11              12  

i want to get child company records of each and every company by using above 2 tables, precisely i want to get result like below 我想通过使用上面的2个表来获取每个公司的子公司记录,恰好我想得到如下的结果

 CompanyID   CompanyName         Level
 1               xyz              0
 2               xyz-c            1
 3               xyz-c1           1
 4               xyz-c2           1
 5               xyz-c-c          2
 6               xyz-c-c1         2
 7               xyz-c-c2         2
 8               xyz-c-c1-c       3
 9               xyz-c-c1-c1      3
10               xyz-c-c1-c2      3

i went through some recursive CTE's butit's not fitting into this, i have multiple sets of parent child (multiple levels) records,so i need a query to fetch all the records in order 我经历了一些递归CTE的问题,但不适合这样做,我有多组父子记录(多个级别),因此我需要查询才能按顺序提取所有记录

with cte as (
    select C.CompanyID, 0 as Level
    from COMPANY as C
    where C.CompanyID not in (select T.ChildCompanyID from COMPANYMAPPING as T)

    union all

    select C.ChildCompanyID as CompanyID, A.Level + 1 as Level
    from cte as A
        inner join COMPANYMAPPING as C on C.ParentCompanyID = A.CompanyID
)
select
    C.CompanyID, C.CompanyName, A.Level
from cte as A
    inner join COMPANY as C on C.CompanyID = A.CompanyID

sql fiddle demo sql小提琴演示

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

相关问题 在存储过程中将多个父子记录插入SQL Server 2008中 - Insert multiple parent child records into SQL Server 2008 in a stored procedure 转储SQL Server 2008 R2中父/子类型记录的完整路径列表 - Dump list of full paths of parent/child type records in SQL Server 2008 R2 检索SQL Server中的父子记录 - Retrieve parent child records in SQL Server 在SQL Server 2008/2012中的单个SQL语句中返回子记录 - Returning child records in a single SQL statement in SQL Server 2008/2012 SQL JOIN PARENT AND CHILD 并获取 PARENT 和 CHILD RECORDS 的计数 - SQL JOIN PARENT AND CHILD and GET count of PARENT and CHILD RECORDS 在迁移数据时创建父子关系(SQL Server 2008) - creating parent child relationship when migrating data (sql server 2008) 如果SQL Server 2008中存在子级,则阻止删除或更新父级 - Prevent deleting or updating the parent if child exists in SQL Server 2008 SQL Query用于获取具有子记录列表的父表的记录 - SQL Query to get records of parent table that have a list of child records SQL Server:如何在自引用表中获取父ID的所有子记录 - SQL Server: How to get all child records given a parent id in a self referencing table SQL Server查询基于父记录的更新子节点 - SQL Server query update child node based on parent records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM