在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)
原文:在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)
所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。
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
我的解法:
-
--drop table tb
-
-
create table tb(ID int, DeprtID int, DeprtName varchar(10))
-
-
insert into tb
-
select 1, 0, '1'
-
union all select 2 , 1 , '2'
-
union all select 3 , 1 , '3'
-
union all select 4 , 2 , '4'
-
union all select 5 , 3 , '5'
-
union all select 6 , 4 , '6'
-
union all select 7 , 5, '7'
-
go
-
-
-
;with t
-
as
-
(
-
select id,DeprtID,DeprtName,1 as level,
-
cast(right('000'+cast(id as varchar),3) as varchar(max)) as sort
-
from tb
-
where DeprtID =0
-
-
union all
-
-
select tb.id,tb.DeprtID,tb.DeprtName,level + 1 ,
-
cast(sort+right('000'+cast(tb.id as varchar),3) as varchar(max))
-
from t
-
inner join tb
-
on t.id = tb.DeprtID
-
)
-
-
select id,deprtid,deprtname
-
from t
-
order by sort
-
/*
-
id deprtid deprtname
-
1 0 1
-
2 1 2
-
4 2 4
-
6 4 6
-
3 1 3
-
5 3 5
-
7 5 7
-
*/
这里还有个例子,就是递归查询后,按照树形来排序:
-
drop table tb
-
-
create table tb
-
(
-
id int,
-
pid int,
-
name varchar(20)
-
)
-
-
insert into tb
-
select 1,null,'x'
-
union all select 2,1,'a'
-
union all select 3,1,'b'
-
union all select 4,2,'aa'
-
union all select 5,3,'bb'
-
go
-
-
-
;with t
-
as
-
(
-
select id,pid,name,1 as level,
-
cast(right('000'+cast(id as varchar),3) as varchar(max)) as sort
-
from tb
-
where pid is null
-
-
union all
-
-
select tb.id,tb.pid,tb.name,level + 1 ,
-
cast(sort+right('000'+cast(tb.id as varchar),3) as varchar(max))
-
from t
-
inner join tb
-
on t.id = tb.pid
-
)
-
-
select *
-
from t
-
order by sort
-
/*
-
id pid name level sort
-
1 NULL x 1 001
-
2 1 a 2 001002
-
4 2 aa 3 001002004
-
3 1 b 2 001003
-
5 3 bb 3 001003005
-
*/
在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)的更多相关文章
- 在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题)
原文:在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...
- 在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时、分钟计算问题)
原文:在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时.分钟计算问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. ...
- 在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据)
原文:在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决 ...
- 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)
原文:在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- 在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字)
原文:在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- 在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存)
原文:在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...
- 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...
- 在论坛中出现的比较难的sql问题:38(字符拆分 字符串检索问题)
原文:在论坛中出现的比较难的sql问题:38(字符拆分 字符串检索问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得 ...
- 在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名)
原文:在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- 在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串)
原文:在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
随机推荐
- MySQL 中视图和表的区别以及联系是什么?
两者的区别: (1)视图是已经编译好的 SQL 语句,是基于 SQL 语句的结果集的可视化的表,而表不是. (2)视图没有实际的物理记录,而基本表有. (3)表是内容,视图是窗口. (4)表占用物理空 ...
- 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 ...
- THINKPHP扩展PHPEXCEL,PHP7.2以上版本无法导出Excel
THINKPHP扩展PHPEXCEL与PHP7.3高版本兼容问题 框架:THINKPHP5,PHPEXCEL版本:1.81 无法导出EXCEL原因为Shared/OLE.php第290行使用cont ...
- linux redis 设置密码:
在服务器上,这里以linux服务器为例,为redis配置密码. 1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,) (1)首先进入redi ...
- Bitmap添加水印效果
package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; i ...
- SpringBoot集成Spring MVC视图
SpringBoot在springmvc的视图解析器方面就默认集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在视图引擎上就已经集成自动配 ...
- 123457123456---com.threeObj03.FanPaiZi01--- 记忆翻牌儿童
com.threeObj03.FanPaiZi01--- 记忆翻牌儿童
- jQuery BlockUI Plugin Demo 2
Overview The jQuery BlockUI Plugin lets you simulate synchronous behavior when using AJAX, without l ...
- 看看BeginInvoke的用法,亲爱的们
看看它是杂带参数的哈 using System; using System.Threading; class MyTest { delegate bool deleTest(string a,stri ...
- 【Leetcode_easy】1078. Occurrences After Bigram
problem 1078. Occurrences After Bigram 题意 solution: class Solution { public: vector<string> fi ...