フォーラムに表示されることがより困難SQL問題:21(再帰ノードの下のすべてのリーフノードを検索)

オリジナル: フォーラムに表示されることがより困難SQLの問題:21(再帰ノードの下のすべてのリーフノードを検索)

最近では、フォーラムでは、私は彼らが解決することができますが、多くの困難なSQLの問題に会ったが、数日後、彼らはの溶液を忘れて、覚えていないことができました。

だから、私は出会いの後にこの問題ことを再度、記録する必要性を感じる、との考えから回答を得ることができます。


問題:SQLを探す:ノードの下にすべてのリーフノードを取得

Departmentテーブル名:tb_department
上述のID INT上記-上記IDのノード
前記ID上記の親ノード- PID INT
キャプションVARCHAR(50) -部署名
----------------------- --------------
上記IDは、キャプションのpid
--------------------------------- -------------
1つの0       AA
20     1      BB
64 20     CC
22     1      DD
23 22    
24 1       FF
25     0      GG
26     1      HH
27     25     II
---------------- ----------------ツリーは、以下

------------ --------------------------
Q:どのようにノードの下にリーフノードのほとんど終わりのすべてを取得します。
たとえば、次のようにテールエンドノードAAのCC、EE、FF、HHの下にあるすべてのノードを取得するには?

2005年の私のソリューションおよび上記のバージョンのSQLサーバー:


   
   
  1. create table tb_department(
  2. id int, --节点id
  3. pid int, --父节点id
  4. caption varchar( 50) --部门名称
  5. )
  6. insert into tb_department
  7. select 1 , 0 , 'AA' union all
  8. select 20 , 1 , 'BB' union all
  9. select 64 , 20 , 'CC' union all
  10. select 22 , 1 , 'DD' union all
  11. select 23 , 22 , 'EE' union all
  12. select 24 , 1 , 'FF' union all
  13. select 25 , 0 , 'GG' union all
  14. select 26 , 1 , 'HH' union all
  15. select 27 , 25 , 'II'
  16. go
  17. ;with t
  18. as
  19. (
  20. select id,pid,caption
  21. from tb_department
  22. where caption = 'AA'
  23. union all
  24. select t1.id,t1.pid,t1.caption
  25. from t
  26. inner join tb_department t1
  27. on t.id = t1.pid
  28. )
  29. select *
  30. from t
  31. where not exists( select 1 from tb_department t1 where t1.pid = t.id)
  32. /*
  33. id pid caption
  34. 24 1 FF
  35. 26 1 HH
  36. 23 22 EE
  37. 64 20 CC
  38. */


それは、SQL Server 2000のであれば、それを作成する方法:


   
   
  1. --1.建表
  2. create table tb_department(
  3. id int, --节点id
  4. pid int, --父节点id
  5. caption varchar( 50) --部门名称
  6. )
  7. insert into tb_department
  8. select 1 , 0 , 'AA' union all
  9. select 20 , 1 , 'BB' union all
  10. select 64 , 20 , 'CC' union all
  11. select 22 , 1 , 'DD' union all
  12. select 23 , 22 , 'EE' union all
  13. select 24 , 1 , 'FF' union all
  14. select 25 , 0 , 'GG' union all
  15. select 26 , 1 , 'HH' union all
  16. select 27 , 25 , 'II'
  17. go
  18. --2.定义表变量
  19. declare @tb table
  20. ( id int, --节点id
  21. pid int, --父节点id
  22. caption varchar( 50), --部门名称
  23. level int --层级
  24. )
  25. --3.递归开始
  26. insert into @tb
  27. select *, 1 as level
  28. from tb_department
  29. where caption = 'AA'
  30. --4.递归的过程
  31. while @@ROWCOUNT > 0
  32. begin
  33. insert into @tb
  34. select t1.id,t1.pid,t1.caption, level + 1
  35. from @tb t
  36. inner join tb_department t1
  37. on t.id = t1.pid
  38. where not exists( select 1 from @tb t2
  39. where t.level < t2.level)
  40. end
  41. --5.最后查询
  42. select *
  43. from @tb t
  44. where not exists( select 1 from tb_department t1 where t1.pid = t.id)
  45. /*
  46. id pid caption level
  47. 24 1 FF 2
  48. 26 1 HH 2
  49. 64 20 CC 3
  50. 23 22 EE 3
  51. */




公開された416元の記事 ウォンの賞賛135 ビュー940 000 +

おすすめ

転載: www.cnblogs.com/lonelyxmas/p/12020035.html