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

推荐订阅源

G
Google Developers Blog
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
D
Docker
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Simon Willison's Weblog
Simon Willison's Weblog
S
Security Affairs
NISL@THU
NISL@THU
T
Tor Project blog
A
About on SuperTechFans
宝玉的分享
宝玉的分享
腾讯CDC
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
Latest news
Latest news
C
Check Point Blog
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
云风的 BLOG
云风的 BLOG
SecWiki News
SecWiki News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
爱范儿
爱范儿
月光博客
月光博客
V
Vulnerabilities – Threatpost
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs

博客园 - 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;