sql problem more difficult to appear in the forum: 21 (recursion retrieve all the leaf nodes under a node)

Original: SQL problem more difficult to appear in the forum: 21 (recursion retrieve all the leaf nodes under a node)

Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.

So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.


Problem: Find the SQL: retrieve all the leaf nodes under a node

Department table name: tb_department
the above mentioned id int - node of the above mentioned id
pid int - the parent node of the above mentioned id
Caption VARCHAR (50) - the department name
----------------------- --------------
the above mentioned id pid Caption
--------------------------------- -------------
1 0       AA
20     1      BB
64 20     CC
22     1      DD
23      22     The
24 1       FF
25     0      GG
26     1      HH
27     25     II
---------------- ---------------- tree follows

------------ --------------------------
Q: how to retrieve all of the most end of the leaf nodes under a node.
For example: to retrieve all the nodes under the tail end node AA CC, EE, FF, HH?

My solution for 2005 and above versions sql server:


   
   
  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. */


If it is sql server 2000, how to write it:


   
   
  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. */




Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12020035.html