SQL - 自联接/递归查询

时间:2016-12-07 09:37:49

标签: sql sql-server-2008 pivot common-table-expression recursive-query

我有一张这样的表

ID  GroupName             ParentID  IsFinal 
1   Operator Interface         0     0  
2   Ways to Access             1     0  
3   Network Communication      1     0  
4   Ways of Alarm Reporting    1     0  
5   Desktop                    2     1  
6   POT                        2     1  
7   LAN                        3     1  
8   SMS                        4     1  

我需要一个结果

ID  GroupName                Parent1          Parent2  ...N..     IsFinal   
1   Operator Interface        NA                NA                 0    
2   Ways to Access          Operator Interface  NA                 0
3   Network Communication   Operator Interface  NA                 0    
4   Ways of Alarm Reporting Operator Interface  NA                 0
5   Desktop                  Ways to Access   Operator Interface    1
6   POT                      Ways to Access   Operator Interface    1   
7   LAN                      Network CommunicationOperator Interface 1  
8   SMS                        ....

我正在进行自我加入,它没有给出理想的结果。

select * from dbo.Table m left outer join dbo.Table n 
on m.ParentID = n.ID

还有其他办法吗?

0 个答案:

没有答案