原文:在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)


最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。

sql2008 树形结构分组
http://bbs.csdn.net/topics/390634930

ID DeprtID DeprtName 
1   0        1        
2   1        2
3   1        3
4   2        4
5   3        5
6   4        6
7   5        7

分组后效果
ID DeprtID DeprtName 
1   0        1        
2   1        2
4   2        4
6   4        6
3   1        3
5   3        5
7   5        7

我的解法:


  1. --drop table tb
  2. create table tb(ID int, DeprtID int, DeprtName varchar(10))
  3. insert into tb
  4. select 1, 0, '1'
  5. union all select 2 , 1 , '2'
  6. union all select 3 , 1 , '3'
  7. union all select 4 , 2 , '4'
  8. union all select 5 , 3 , '5'
  9. union all select 6 , 4 , '6'
  10. union all select 7 , 5, '7'
  11. go
  12. ;with t
  13. as
  14. (
  15. select id,DeprtID,DeprtName,1 as level,
  16. cast(right('000'+cast(id as varchar),3) as varchar(max)) as sort
  17. from tb
  18. where DeprtID =0
  19. union all
  20. select tb.id,tb.DeprtID,tb.DeprtName,level + 1 ,
  21. cast(sort+right('000'+cast(tb.id as varchar),3) as varchar(max))
  22. from t
  23. inner join tb
  24. on t.id = tb.DeprtID
  25. )
  26. select id,deprtid,deprtname
  27. from t
  28. order by sort
  29. /*
  30. id deprtid deprtname
  31. 1 0 1
  32. 2 1 2
  33. 4 2 4
  34. 6 4 6
  35. 3 1 3
  36. 5 3 5
  37. 7 5 7
  38. */

这里还有个例子,就是递归查询后,按照树形来排序:


  1. drop table tb
  2. create table tb
  3. (
  4. id int,
  5. pid int,
  6. name varchar(20)
  7. )
  8. insert into tb
  9. select 1,null,'x'
  10. union all select 2,1,'a'
  11. union all select 3,1,'b'
  12. union all select 4,2,'aa'
  13. union all select 5,3,'bb'
  14. go
  15. ;with t
  16. as
  17. (
  18. select id,pid,name,1 as level,
  19. cast(right('000'+cast(id as varchar),3) as varchar(max)) as sort
  20. from tb
  21. where pid is null
  22. union all
  23. select tb.id,tb.pid,tb.name,level + 1 ,
  24. cast(sort+right('000'+cast(tb.id as varchar),3) as varchar(max))
  25. from t
  26. inner join tb
  27. on t.id = tb.pid
  28. )
  29. select *
  30. from t
  31. order by sort
  32. /*
  33. id pid name level sort
  34. 1 NULL x 1 001
  35. 2 1 a 2 001002
  36. 4 2 aa 3 001002004
  37. 3 1 b 2 001003
  38. 5 3 bb 3 001003005
  39. */
发布了416 篇原创文章 · 获赞 135 · 访问量 94万+

在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)的更多相关文章

  1. 在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题)

    原文:在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...

  2. 在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时、分钟计算问题)

    原文:在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时.分钟计算问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. ...

  3. 在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据)

    原文:在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决 ...

  4. 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)

    原文:在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  5. 在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字)

    原文:在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  6. 在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存)

    原文:在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...

  7. 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)

    原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...

  8. 在论坛中出现的比较难的sql问题:38(字符拆分 字符串检索问题)

    原文:在论坛中出现的比较难的sql问题:38(字符拆分 字符串检索问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得 ...

  9. 在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名)

    原文:在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  10. 在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串)

    原文:在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

随机推荐

  1. MySQL 中视图和表的区别以及联系是什么?

    两者的区别: (1)视图是已经编译好的 SQL 语句,是基于 SQL 语句的结果集的可视化的表,而表不是. (2)视图没有实际的物理记录,而基本表有. (3)表是内容,视图是窗口. (4)表占用物理空 ...

  2. Wamp 升级php7.3报错

    电脑系统:win10 Wamp版本: WampServer Version 3.0.4 32bit Apache 2.4.18 - PHP 7.3.7 - MySQL 5.7.11 PHP 5.6.1 ...

  3. THINKPHP扩展PHPEXCEL,PHP7.2以上版本无法导出Excel

     THINKPHP扩展PHPEXCEL与PHP7.3高版本兼容问题 框架:THINKPHP5,PHPEXCEL版本:1.81 无法导出EXCEL原因为Shared/OLE.php第290行使用cont ...

  4. linux redis 设置密码:

    在服务器上,这里以linux服务器为例,为redis配置密码. 1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,) (1)首先进入redi ...

  5. Bitmap添加水印效果

    package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; i ...

  6. SpringBoot集成Spring MVC视图

    SpringBoot在springmvc的视图解析器方面就默认集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在视图引擎上就已经集成自动配 ...

  7. 123457123456---com.threeObj03.FanPaiZi01--- 记忆翻牌儿童

    com.threeObj03.FanPaiZi01--- 记忆翻牌儿童

  8. jQuery BlockUI Plugin Demo 2

    Overview The jQuery BlockUI Plugin lets you simulate synchronous behavior when using AJAX, without l ...

  9. 看看BeginInvoke的用法,亲爱的们

    看看它是杂带参数的哈 using System; using System.Threading; class MyTest { delegate bool deleTest(string a,stri ...

  10. 【Leetcode_easy】1078. Occurrences After Bigram

    problem 1078. Occurrences After Bigram 题意 solution: class Solution { public: vector<string> fi ...