惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
V
V2EX
C
Check Point Blog
GbyAI
GbyAI
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
T
Troy Hunt's Blog
博客园 - Franky
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Security Blog
Microsoft Security Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
The Cloudflare Blog
S
SegmentFault 最新的问题
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
I
InfoQ
博客园 - 【当耐特】
NISL@THU
NISL@THU
A
About on SuperTechFans
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Scott Helme
Scott Helme
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
Security Archives - TechRepublic
Security Archives - TechRepublic
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
IT之家
IT之家
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
aimingoo的专栏
aimingoo的专栏
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
大猫的无限游戏
大猫的无限游戏
S
Security Affairs
The Register - Security
The Register - Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 热门话题
T
Tor Project blog

博客园 - Amwpfiqvy

如何查看执行计划 SQL Server 堆表与栈表的对比(大表) SQL Server中CURD语句的锁流程分析 SQL:查询购买了所有指定商品的人 在SQL Server中使用正则表达式 查看SQL Server性能时常用的性能计数器 SQL Server中行列转换 Pivot UnPivot Apq本地工具集 Apq.aspx 第一个:_Config.js JScript.Encode.js - Amwpfiqvy - 博客园 Apq.Threading.js - Amwpfiqvy - 博客园 Script/_Config.js Apq.Text.js Apq.js - Amwpfiqvy - 博客园 prototype.js Apq.aspx - Amwpfiqvy - 博客园 查看数据库所有用户表及其列信息 利用JScript的Literal Syntax特性用字符串表示对象
树表分级统计
Amwpfiqvy · 2014-02-17 · via 博客园 - Amwpfiqvy

针对树表的分级统计,Oracle中可以用CONNECT_BY_ROOT轻松实现,而SQL Server则没有简单类似的语法,借鉴其思路可使用以下CTE查询实现类似CONNECT_BY_ROOT的功能:

-- 0.准备测试数据
CREATE TABLE #data (id int,ParentID int,value int);

INSERT INTO #data
      SELECT 1 , 0, 10
UNION SELECT 2 , 0, 20
UNION SELECT 3 , 1, 30
UNION SELECT 4 , 3, 40
UNION SELECT 5 , 4, 50
UNION SELECT 6 , 2, 60
UNION SELECT 7 , 3, 70
UNION SELECT 8 , 7, 80
UNION SELECT 9 , 8, 90
;

DECLARE @RootID int;  -- 这里使用变量,实际使用时一般为参数

DECLARE @RootLevel int;
SELECT @RootID = 3;

-- 计算传入结点的Level(根结点为1)
;WITH _P AS (
SELECT ParentID,id FROM #data WHERE id = @RootID
UNION ALL
SELECT a.ParentID,a.id
  FROM #data a INNER JOIN _P cte ON a.id = cte.ParentID
)
SELECT @RootLevel = count(*) FROM _P;

;WITH
-- 1.取到所有下级
_tree AS (
SELECT ParentID,id,@RootLevel Level, CASE WHEN EXISTS(SELECT 1 FROM #data t1 WHERE t1.ParentID = t.id) THEN 0 ELSE 1 END isLeaf/*是否为叶子结点(无下级)*/
  FROM #data t WHERE id = @RootID
UNION ALL
SELECT a.ParentID,a.id, cte.Level + 1, CASE WHEN EXISTS(SELECT 1 FROM #data t1 WHERE t1.ParentID = a.id) THEN 0 ELSE 1 END
  FROM #data a INNER JOIN _tree cte ON cte.id = a.ParentID
),
-- 2.再展开每一行的所有下级
_t AS(
SELECT a.id PertainID/*所属上级*/,a.id, a.Level, a.isLeaf FROM _tree a
UNION ALL
SELECT cte.PertainID,a.id, a.Level, a.isLeaf
  FROM _tree a INNER JOIN _t cte ON cte.id = a.ParentID
)
--_t这个公用表即为类似CONNECT_BY_ROOT的结果集,我们可以利用_t这个公用表结合GROUP BY方便的统计出每级的聚合结果。
--简单查看
--SELECT * FROM _T ORDER BY 1,3;

-- 3.分级统计
SELECT PertainID, sum(t.value) 合计
  FROM _t INNER JOIN #data t ON _t.id = t.id
-- WHERE _t.isLeaf = 0 -- 统计到非叶子结点(具有下级才算统计结果)
 GROUP BY PertainID;

DROP TABLE #data;