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攻击问题.) 前言 本文 ...
随机推荐
- 编程开发之--Oracle数据库--存储过程使用动态参数绑定(3)
1.动态参数绑定,可以实现动态的执行不同的sql --创建包 create or replace PACKAGE MYPACKAGE AS type empcursor is ref cursor; ...
- Angular material mat-icon 资源参考_Action
ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...
- ifconfig无输出的原因及解决办法
问题 执行 ifconfig 命令无任何报错,也无任何输出信息 [root@linuxprobe ~]# ifconfig[root@linuxprobe ~]# 排错 1. 检查PATH变量 [r ...
- 学习总结 —— python
1.了解python 学习python 3 入门知识 python 库 .包 .模块 2.了解pycharm Pycharm 导入 Python 包.模块 pycharm 快捷键 3.了解djan ...
- apache2 + django 路径问题
问题: 在代码中使用sys.path.append(), 添加模块路径后,仍然报错找不到包. 虽然在LD_LIBRARY_PATH中配置了.so文件打路径,仍然报错找不到. 原因: 检查apahce2 ...
- String数据转Matrix矩阵
String数据转Matrix矩阵 private Matrix String_To_Matrix(string str) { int[] Remove_Num = new int[10]; int ...
- Rational Rose2007下载和安装
网上关于Rational Rose2007安装包,网上找了一堆大多都是垃圾,最后找到一个可用的(带激活文件),保存在自己的网盘里,这里分享出来:https://pan.baidu.com/s/1bpb ...
- Asp.Net webconfig中使用configSections的用法
最近闲来无事,研究研究公司的框架,无意中打开了webconfig页面,发现了一个我不认识的节点<configSections></configSections>,于是百度之,大 ...
- hadoop集群搭建过程中遇到的问题
在安装配置Hadoop集群的过程中遇到了很多问题,有些是配置导致的,有些是linux系统本身的问题造成的,现在总结如下. 1. hdfs namenode -format出现错误:hdfs namen ...
- echart使用设置一个柱形的最小宽度
因为echart的横坐标的个数不同会影响柱形图的宽度 如果只有三个月的就会是这样的 这样一来效果就不是很好,所以想做成如下效果 思路: 只是需要向xDate的值设置成想要的长度,如上图就是设置12,如 ...