Recursive sum in parent-child hierarchy T-SQL
---树形(父子关系类)分级类统计(父子统计)
--涂聚文 2014-08-14
drop table BookKindList create table BookKindList
(
BookKindID INT IDENTITY(1,1) PRIMARY KEY,
BookKindName nvarchar(500) not null,
BookKindParent int null
)
GO drop table BookCostsPer
---
CREATE TABLE BookCostsPer
(
ID INT IDENTITY(1,1) PRIMARY KEY,
NodeId INT NOT NULL,
[BookName] nvarchar(500) NOT NULL,
[CostsValue] DECIMAL(18,6) NOT NULL,
CostDate datetime default(getdate())
)
go select * from BookKindList insert into BookKindList(BookKindName,BookKindParent) values('塗聚文书目录',null)
insert into BookKindList(BookKindName,BookKindParent) values('文学',1)
insert into BookKindList(BookKindName,BookKindParent) values('设计艺术',1)
insert into BookKindList(BookKindName,BookKindParent) values('自然科学',1)
insert into BookKindList(BookKindName,BookKindParent) values('小说',2)
insert into BookKindList(BookKindName,BookKindParent) values('诗词散曲',2) insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(3,'设计理论',450,'2014-01-02')
insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(4,'计算机科学',400,'2014-01-02')
insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(5,'傲慢與偏見',550,'2014-01-02')
insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(6,'宋词',150,'2014-01-02')
insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(3,'版式设计',150,'2013-05-02')
insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(4,'C语言设计',200,'2013-05-02')
insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(5,'汤姆叔叔的小屋',530,'2013-05-02')
insert into BookCostsPer(NodeId,[BookName],[CostsValue],CostDate) values(6,'唐诗',110,'2013-05-02') --视图
create view v_BookCostsPer
as
select *,year(CostDate) as 'YearName' from BookCostsPer
go ---統計
WITH DirectReport (BookKindParent, BookKindID, [BookKindName], LEVEL, Struc)
AS
(
-- anchor
SELECT a.BookKindParent, a.BookKindID, a.BookKindName, 0 AS LEVEL, cast(':' + cast(a.BookKindID AS varchar) + ':' AS varchar (100)) AS Struc
FROM BookKindList a
WHERE a.BookKindParent IS NULL
UNION ALL
-- recursive
SELECT a.BookKindParent, a.BookKindID, a.BookKindName, LEVEL +1, cast(d.Struc + cast(a.BookKindID AS varchar)+ ':' AS varchar(100)) AS Struc
FROM BookKindList a
JOIN DirectReport d ON d.BookKindID = a.BookKindParent
)
SELECT d.BookKindParent, d.BookKindID, d.BookKindName, d.level, d.Struc,
sum(CASE WHEN d.Struc = SUBSTRING(dd.Struc, 1, len(d.Struc))THEN c.CostsValue ELSE 0 END) AS TotCost
FROM DirectReport d,DirectReport dd
JOIN BookCostsPer c ON c.NodeId = dd.BookKindID
GROUP BY d.BookKindParent,d.BookKindID, d.BookKindName, d.level, d.Struc
ORDER BY d.BookKindID
GO -----按年各父子类合计
with DirectReport (BookKindParent, BookKindID, [BookKindName], Level, Struc, [YearName])
as
(
-- anchor
select a.BookKindParent, a.BookKindID, a.BookKindName, 0 as Level, cast(':' + cast(a.BookKindID as varchar) + ':' as varchar (100)) as Struc, y.[YearName]
from BookKindList a, YearNames y
where a.BookKindParent is null
union all
-- recursive
Select a.BookKindParent, a.BookKindID, a.BookKindName, Level +1, cast(d.Struc + cast(a.BookKindID as varchar)+ ':' as varchar(100)) as Struc, d.[YearName]
from BookKindList a
join DirectReport d on d.BookKindID = a.BookKindParent
) Select d.BookKindParent, d.[YearName], d.BookKindID, d.BookKindName, d.level, d.Struc,-- dd.Struc,
sum(case when d.Struc = SUBSTRING(dd.Struc, 1, len(d.Struc))then c.CostsValue else 0 end) as TotCost
from DirectReport d
left join DirectReport dd on d.[YearName] = dd.[YearName]
join v_BookCostsPer c on c.[YearName] = dd.[YearName] and c.NodeId = dd.BookKindID
group by d.BookKindParent, d.[YearName], d.BookKindID, d.BookKindName, d.level, d.Struc
order by d.[YearName], d.BookKindID
GO
Recursive sum in parent-child hierarchy T-SQL的更多相关文章
- [NHibernate]Parent/Child
系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...
- 服务启动Apache服务,错误Parent: child process exited with status 3 -- Aborting.解决
不能启动apache,或者使用wamp等集成包后,唯独apache服务启动后有停止,但是把东西搬到其他机器上却没事问题可能和网络有关,我查了很多资料首先找打apache的错误报告日志,发现现实诸多的调 ...
- XPath学习:parent,child
XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...
- csharp: DataRelation objects to represent a parent/child/Level relationship
/// <summary> /// /// </summary> /// <param name="sender"></param> ...
- 【转载】Analysis Service Tabular Model #003 Multidimensional Model VS Tabular Model 我们该如何选择?
由于Multidimensional Model 和 Tabular Model 并不能互相转换, 所以在项目之初就应该要考虑好选择哪一种模型进行开发. 以下只是一些建议: Licensing 许可和 ...
- [转]Dynamic SQL & Stored Procedure Usage in T-SQL
转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...
- SQL Server 查询请求
当SQL Server 引擎接收到用户发出的查询请求时,SQL Server执行优化器将查询请求(Request)和Task绑定,并为Task分配一个Workder,SQL Server申请操作系统的 ...
- 基于Oracle的SQL优化(崔华著)-学习笔记
201704171025 01. 列rows记录的就是执行计划中每一个执行步骤所对应的Cardinality的值 列Cost(%CPU)记录的就是执行计划中的每一个执行步骤对应的成本 02. Comp ...
- 【spring】(填坑)sql注入攻击 - 持久层参数化
结果 填坑失败,并没有看懂是如何检测sql攻击的. 只能说的是: 建议都使用参数化传递sql语句参数.(所以,用hibernate.mybatis等框架的真不用太担心sql攻击问题.) 前言 本文 ...
随机推荐
- Struts2框架里面action与前端jsp页面进行交互路径问题---》一个对话框里面有很多超链接,进行相应的跳转
一个对话框里面有很多超链接,右边是点击超链接跳转到的相应页面(在一个页面上就相当于点击该超链接时候,就把该简短页面置顶):这个问题困扰我两天:还请大神给我解决,也没有解决,我仔细对比了相关路径,后面添 ...
- short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
对于 short s1 = 1; s1 = s1 + 1;由于 s1+1运算时会自动提升表达式的类型,所以结果是 int型,再赋值给 short 类型 s1时, 编译器将报告需要强制转换类型的错误.对 ...
- 基础篇:6.9)GD&T较线性尺寸公差的优缺点
本章目的:理解GD&T标注对比线性/传统/坐标尺寸公差的优势,但也不要忘记其使用限制. 1.线性尺寸公差 1.1 定义 线性尺寸公差=传统尺寸公差=坐标尺寸公差. 传统尺寸公差(Tradi ...
- python 封装,隐藏属性,绑定方法classmethod和staticmethod
[封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. [好处] 1. 将变化隔离: 2. 便于使用: 3. 提高复用性: 4. 提高安全性: [封装原则] 1. 将不需要对外提供的内容都隐藏起 ...
- Python——内部参数对外部实参的影响
无论函数传递的参数的可变还是不可变,只要针对参数使用赋值语句,会在函数内部修改局部变量的引用,不会影响到外部变量的引用,而如果传递的参数是可变类型,在函数内部使用方法修改了数据的内容,同样会影响到外部 ...
- c# 线程,同步,锁
最近在阅读<c#高级编程> 这本书.记录一下关于锁的使用 大致分为三种方法: 方法1:使用 lock 方法2:使用 Interlocked 方法3:使用 Monitor using Sys ...
- C++下混合编译c语言方法总结
最近在读SGI STL源码,感觉对C++的学习很有帮助,之前对于泛型.iterator.traits等等各种特性的概念非常模糊,通过这两天的琢磨,再加上<STL 源码剖析>的帮助,对C++ ...
- JavaSE_XMind总结
1 JDBC知识点总结
- HTML5开发手机项目-个人总结(转)
让网页的宽度自适应屏幕<meta name="viewport" content="width=device-width"/> 1)html上 ...
- 用eclipse查看JDK源代码
把jdk的源代码导入eclipse