sqlserver cte递归向上统计
数据字典如下
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATcAAABqCAIAAABWJ4H4AAAK6UlEQVR4nO2cz27jOBKH+U6DFtKvMrdRGv0ac3YcoM97832Ajr2HPu2lX2AXncR7aSCJO/9m9rBR0klsyzI4B8kyLZIlSqFEslwfiECiSpRUqZ+Kkimyx94ZjUb9H5QgwoXNF2nPZTQaccKMp6cn16cQDIh9RSr1GsSRZx3EviKVes3j46PrUwgGxL4ilXoN4sizDmJfeaPS75/ev//03e7FddFmv6gjL/zr6oL2Ks396bFXSaVekySJojb86+oCta9kZO+RSkmlb+Hh4UFRG/51dYHaVyaQSpUqXa2yav30OIqOzxfpfPKBsYOjyXEUfYgjxhiLJ/89KhZy43/GrCCepPPpcVSsfRiXTW0MWN7mtubgaNr39bYuWba+//Ov9r6aHkfR8dGh4KtFej48YKIrpno/B+U00FfFBYpu2Y0KxqLj8wYR2Hdxo9J0lb3OlzvlfBhFw7PzYcQOx/kqOxicL1/Hh6xciIZn8+kgYvG43HESs2L17OiAxROxpmhzPonzNrc1y+rRvSxZtr69u2/vq/NhVLqi8J7QyPiQxRO9nwNzGuSr/LryS2aH421U7MaJaQQ6uDqfVMoYkyOjslCJGHE192+lZhOsAptDeF/W6/XN7Z0dX4mbtglEsJH3DcppkK+2ATOJ2cFgrHGOoVddXJ0jlaarl9fFTjkbRtHwZBCxeFKunsoL5aqw12m5zA5PKjXyLuGUbL2+vrmz46uyhkWDM5VzYD97X2BfbS5kErMoV6naOSZedXF1fqn09HVxErNoMNX7KO/xljvm/ZbFy+vitPDvJN4E4ukgYtHwVLAJq9RGXo2vwBtW4RytnwNzmpFKx4fCJUtxYhqBDq7OjUqX6er5Zb5TTo+i6Ojby/z5ZfwbY1EcF6tlvbiw6Yf9diKuxp/zpk6Kt0s7jWy7eflRAihZtp79uGnvq63ZdtPnzZu3Yi+lewN0GuSrbb89/ixeYCVOzCOw9+JGpYtl+vP5lQpcsmx9NbsmX73JV98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bWqVAzQIDCQ0tDEcdVgLqbRrlbqanczT2ahKZJUCsa7bqlQON8ta5iqt2MgL5gdqh0qlSGLGvUodzk7m7WxUJUAubbQqIxsAIqw1UN4sKnsZ3jIAG5hE+eUaiphxr9LiKh3M1eDvt/kl7VQKJ9JK9Ms7ck0mrFTqGpdt4BOuNFJ7X9ChyKVYYsZjlXY+75G/89yUNO3xckmEld2b1suaUYoNvimIJyYbyGa156bEVKUBxgypFLZxTG0ulXMXN/goVN5RSWUvpf5rhVSbSystA+cMQyqlHq8bTHq8uqRXAdgdUIV5s7p2THJppUa+IhOox9ubSnudnczb2ahK2v1eWmvAzXIpb6hSeK+mdxmrKg07Ztyr1OnsZJ7ORlXS7vdSWE6V1bc3W4pKd8sQLWvvGtySSjHFjHuVEgCtc6nclHWVAgfVqbTR+TTVKo1qIJW6wVYuFRVlonbzO0LlELpbhuGdBb4RwJBKSaVuQBx51kHsK1Kp1yCOPOsg9hWkUpqdzDmII886iH1Fs5MRhO9AuZRmJ3NOgjc/WAexr+i51GsQR551EPvqb/7wGBjVT4r9AAAAAElFTkSuQmCC" alt="" />
通过sql可以得到如下结果
select yskm,SUM(je) as je from view_dj where swdjh='321311570376851' group by yskm
现在项目有个要求,要统计出他们父级科目的金额。
那怎么向上推出他的父级科目代码的金额,
解决思路:
- 创建一张临时表来保存结果
- 用sqlserver cte 来查询出自己及父级所有的科目代码,代码如下
with cte as
(
select a.kmcode,a.kmname,a.pidkm from sys_km a where kmcode in
( select t1.yskm from view_dj t1 where t1.swdjh='' )
union all
select k.kmcode,k.kmname,k.pidkm from sys_km k inner join cte c on c.pidkm = k.kmcode
)
select * from cte查询结果如下:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY0AAACdCAIAAADHZLOpAAATnklEQVR4nO2dzXLbthbH8TzZdZNh9QZ5g6wlj/oQ2XVX2Z7xAyRP4HEjxTO+m77DnfojG8/cfrf3ruqkseUo8vAuRFEQgAOAIA+BQ5zfcDoUBIHngOA/AE3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ikkKDVV7SizQf2HQ5hkEkhQauvaEWbD+w7zDA7Yl+PjBlxenYeOwaGYRgbrFMMw6QO6xTDMKnDOsUwTOr8H6/Hoxjxrq5sAAAAAElFTkSuQmCC" alt="" />
3.在通过cte和金额表关联把数据插入到临时表中代码如下
insert into #tmphjcx select * from ( select distinct a.kmcode,a.kmname,a.pidkm,b.hjje,b.guoshui,b.dishui from cte a left join( select t2.kmcode,(t2.kmcode+'_'+t2.kmname) as kmname,hjje=sum(je), guoshui=sum(case when t1.ic='1' then je end),dishui=sum(case when t1.ic='2' then je end) from view_dj t1 left join sys_km t2 on t2.kmcode=t1.yskm where t1.swdjh=@bm
group by t2.kmcode,t2.kmname)b
on b.kmcode=a.kmcode)c
查询结果如下
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASsAAACrCAIAAACxAS2PAAAQ10lEQVR4nO2dT2/byN3H5/XsLSCwoPUO+g4WuSwqGbMvoocAe7OsALokAIEG0CHI5UE3sh9Ae9mD9xoTKOq/RQV362a7feqkiJzEsiLLmOcwEjUkZ8ghORQ59PcDQqDGQ+o3Ij8ekuL8SCYAgOogVQcAwIMGBgJQJTAQgCohk8lkp/fM0qnqbw+AoiwNZBYCA0EDsNvA+k9Vb19Qd+w2cFFvYCBIJcVA6vnSyaHRaZPucXZ6zz6GOTg4ODg42A8zGLz8WAUwEOiQbqDns+XkMaqYkg0khCS8NWig50XDuLq6op4HA0FtydwHxns/h/qOU8hAoibZwOswgYHX19f+e+a/Z6/eTCj1rqvg7u4OBoJUUgwUe5XDw8Ng/uDggM/MZjPP8x2HpuqXIJhommpeamCkMTza8/Nz3/cPDw9fvZl4nk+pV+gLIsqfTBP+NJlMYCDQIYOBgXWMsf39fa4f73m2trZS9UvwKncf+CGM5/me7+/v73uePxi85BOl3gdtCCE6MyrEVcFAoEOKgYPBy/Pz88PDw4MwfC/n1zmSDYyLJDUwdV5q4H/DxM8DX72ZLDtD3/d8/+Li4r9pEELEGfE1UkG1IGc+n8NAkEq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0IPcNBjYd3SwVP7SXV2LqlqVCu6RE9Ay8HPed9S8T7Od1jibx5wox5wQdSEpgYLNAlgoD4J4YkBtkqTAADAS5QZYKA8BAkBtkqTAADAS5QZYKA8BAkBtkqXjQVLjnAQ7GBxrA0kZZGnbDgIEGsLRRlobdMGCgASxtlKVhNwwYaABLG2Vp2A1D20D9JwklkrbVy7srTb5mI7euJTRK9Rgj8amgwaj++CjohCc6FQ8dBtaBDHligsfpFRkur97q5WWpiJWs22Dm9lFVo4Tn94Uf5Tfut0j8IX/Rr1ayuPbzcYuEDTaJnoEFtRPI0AcaHp2kHHlYXEGdXVn40OWz7uMqJfw/SHsecFlhg7LRylJx0tsinf0iySlqkKUivPMKHVDx/y0au7LQn40ooaOIkOGn/yoX139CtaGwQelojZE/3nXdzvYyVUxnL/cA+UrHyEvPA4W+qACpu7Ig+jKMUIuWh8nKQILFYWDz0DXQ/jwx8h7GyPF1cqPEflY8o018anzK4sra5sIGm0EvS8WwE2SmsDZLhdTA8AWSvCQ0KnyYO+6HxtuSVn+cLFX0KBnngY1DM0/MXpt0Xk9nN9PTrkvaQxuzVIR61/XVxzKvhSasf92k8Elp9GeXyOK4Fto4dLNUfH7dWf7fbg/tzFJRwe+BYq7/yHmeKFCsmctQpYvj98CGgSwVBrC0UZaG3TCQpcIAljbK0rAbBrJUGMDSRlkadsNAlgoDWNooS8NuGMhSYQBLG2Vp2A0DWSoeLhXudiBAYmCF0QDw0ICBAFQJDASgSmAgAFUCAwGoEhgIQJXAQACqBAYCUCUwEIAqgYEAVMn/A/X8w3ULnLn5AAAAAElFTkSuQmCC" alt="" />
4.看上面的临时表的结果,可以看出科目代码顺序现在是对的,但是他们父级科目的金额都为空的,怎么根据下一级的科目金额获取上一级的科目金额呢
5,这时我们应该按科目代码的长度倒序排列,逐个更新金额,应该我们只有计算出上一级的金额,然后再上一级金额的基础上计算出下一级金额。(注意:逻辑思想很重要)
select kmcode,hjje from #tmphjcx order by len(kmcode) desc
查询结果如下
6。这时我们再更新上面临时表中父级科目金额(创建游标遍历金额表,和临时表关联来更新金额表的值)
declare @kmcode varchar(200)
declare @hjje decimal(18,2)
declare s cursor --declare 创建游标
static
for select kmcode,hjje from #tmphjcx order by len(kmcode) desc
open s --打开游标
fetch next from s into @kmcode,@hjje --提取上次提取行的下一行
while(@@fetch_status = 0)
begin
if( @hjje is null)
begin
update a set a.hjje= b.hjje,a.guoshui=b.guoshui,a.dishui=b.dishui
from #tmphjcx a
left join(select sum(hjje) as hjje,sum(dishui) as dishui,sum(guoshui) as guoshui,@kmcode as kmcode from #tmphjcx b where pidkm=@kmcode ) b
on b.kmcode=a.kmcode where a.kmcode=@kmcode
end
fetch next from s into @kmcode,@hjje
end
close s --关闭游标
deallocate s --删除游标,释放资源
7.查询出我们想要的结果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj0AAAChCAIAAACjyJlHAAAc30lEQVR4nO2d244jt7WG+VBJozzvEhiWBv0gAQLY6h5gbvIKBoKdPa32BvQMuQhguE83ApI4cRzviw3PdLeOrUHtC0ksFg+Lqw5UrcVeH4iBhsVi/Yss8S+Wuorqm8s/lkf+67//pxQEQRAEwijLtyRJkiRJkiTKqeZbHwVBEASBNuJbgiAIAifEtwRBEAROyO9bkiRJkiSJU7J9K9Xff5RlWZaPj49J6x+ELIMSNLz6l5daDWXZlLXB8FUeRXyrK1kGJWh49S8vtRrKsilrg+GrPMpJfevTp09J6x+ELIMSNLz6l5daDWXZlLXB8FUeRXyrK1kGJWh49S8vtRrKsilrg+GrPMqgvjV//+bN+3mKI6Wr2eF0QQlD0ObLvz8HhjgTmJ6NSUbYnmKHtAGHINDy4lv98PHjx9r/s/Ct0wUlDIHdvy5ujw/nW/GzcThtAPFGbgEmRkQZSBvQmD0dvQtJWpUGJ/WtX3/9tfb/LHzrdEEJQ2D3L4bhvIHp2dimkaP0FDukjfZ8K0mr0sD2rZeX3Wq9TZF2u8//+eV/a/XfvyuKd3fr7Wr93VgpVby7u39XFG/HhVJKF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QWlSf894RcmzQLPL612b48L1a1dDU+jG2jqb2pSeL1qBOS/ILyGlLpcyw3P+R/eG9j7Fs992+fame+VeH3mGOko7+aybi7B4LtUzYgySugR0lRbXybNAN86x1vtk/PyxQpy6bkFZTrCkCO158wdZr5rhuFvM3diwK8+peXWg1l2ZS1ATCVjcTnW+vt49MiRcqyKUkGZV3MHa7fkMZj+YoJZnegWrcer0sBxzo9JPs3CC+1GsqyKWsDYCobice3VuvNp8fnFCnLpswyKEHDq395qdVQlk1ZGwBT2Ug8vnWfjCybMsugBA2v/uWlVkNZNmVtAExlI/G+LyMVWTZllkEJGl79y0uthrJsytoAmMpG4vEtQRAEgTUDmsoJsH1rQCmCIAiCEEV8SxAEQeCE+JYgCILACfEtQRAEgRPiW4IgCAInxLcEQRAETohvCYIgCJwQ3xIEQRA4Ib4lCIIgcEJ8SxAEQeCE+JYgCILACfEtQRAEgRPiW4IgCAInxLcEQRAETohvCYIgCJwQ3xIEQRA4Ib4lCIIgcEJ8SxAEQeCE+JYgCILACfEtQRAEgRPiW4IgCAInxLcEQRAETvw/00eYofejvMEAAAAASUVORK5CYII=" alt="" />
结果如下 最后:好的想法+技术可以解决一切难题。如要转载请保留原文地址
sqlserver cte递归向上统计的更多相关文章
- SQL Server中公用表表达式 CTE 递归的生成帮助数据,以及递归的典型应用
本文出处:http://www.cnblogs.com/wy123/p/5960825.html 我们在做开发的时候,有时候会需要一些帮助数据,必须需要连续的数字,连续间隔的时间点,连续的季度日期等等 ...
- TSql CTE 递归原理探究
CTE是如何进行递归的?产生递归的条件有三个,分别是 初始值 自身调用自身 结束递归的条件 1,示例代码 ;with cte as ( as jd union all as jd from cte ) ...
- 8第八章CTE递归及分组汇总高级部分(多维数据集)(转载)
8第八章CTE递归及分组汇总高级部分(多维数据集) 这里贴图太麻烦...算了 UNION 等集合操作符: UNION 等以第一个 SELECT 的 列明 作为 整个结果集的列明,整个结果集 唯一认可 ...
- 第八章 CTE 递归 及 分组汇总 高级部分(多维数据集)
UNION 等集合操作符: UNION 等以第一个 SELECT 的 列明 作为 整个结果集的列明,整个结果集 唯一认可的 唯一逻辑处理阶段 是 ORDER BY 这个意思是说 只有 ORDER ...
- SQL 递归查询,意淫CTE递归的执行步骤
今天用到了sql的递归查询.递归查询是CTE语句with xx as(....)实现的. 假如表Category数据如下. 我们想查找机枪这个子分类极其层次关系(通过子节点,查询所有层级节点).以下是 ...
- sqlserver 树结构递归(向上递归和向下递归)
--获取当前及以下部门 Create proc GetCurrentAndUnderOrg @orgId int as begin WITH cte AS ( SELECT * ,0 AS level ...
- SqlServer:CTE函数处理递归(WITH语法)
我们在做分类处理的时候,总会遇到递归的处理,比如说地区就是一个例子,中国--北京--西城区,我们可以把这样的信息存储在一个数据表中,用ParentID区分根节点和叶子节点.假如我们要做导航,得到了”西 ...
- SQL笔记 - CTE递归实例:显示部门全称
昨天在整理JS的Function时,示例是一个递归函数.说起递归,想起前段时间在搞CTE,那个纠结呀,看似容易,可我总抓不住门道,什么递归条件,什么结束条件,一头雾水...今天一大早就爬起来,果然不负 ...
- Sql Server CTE递归
WITH cte AS ( SELECT a.FNUMBER,a.FMATERIALID AS MainId,b.FMATERIALID AS ChileID,CAST(b.FMATERIALID A ...
随机推荐
- 原生JavaScript的省市县三级联动
三级联动是我们写表单时必不可少的,比如在写收货地址时,就用到他了,最近在看原生JavaScript,从基础写起,待完善,以后再写个jquery版的. <!DOCTYPE html> < ...
- list去除重复
1. [代码][Python]代码 简单去重 ? 1 2 3 4 5 l = [1,2,3,3] l = list(set(l)) >>>l >>>[ ...
- Away3D ATFTexture
之前在项目中贴图大量使用了 PNG 和 jpg 遇到了个问题.在使用BitmapTexture的时候发现 是必须MIP 不管你 是否开启或者关闭 MIP 他都会去创建.而每次MIP都会根据贴图大小去生 ...
- 如何制作iso文件
UltraISO 9.6.2.3059中文完美破解安装版 http://www.upantool.com/qidong/2011/UltraISO_v9.5.0.2800.html 软碟通v9.6.2 ...
- [RxJS] Filtering operators: throttle and throttleTime
Debounce is known to be a rate-limiting operator, but it's not the only one. This lessons introduces ...
- [AngularJS + Unit Testing] Testing Directive's controller with bindToController, controllerAs and isolate scope
<div> <h2>{{vm.userInfo.number}} - {{vm.userInfo.name}}</h2> </div> 'use str ...
- [RxJS] map vs flatMap
What's the difference between map and flatmap? First, let's show what map is. To show that, I need a ...
- android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)
自定义一个dialog: 之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗? ...
- Android热门网络框架Volley详解
.Volley简介 volley的英文意思为‘群发’.‘迸发’.Volley是2013年谷歌官方发布的一款Android平台上的网络通信库.Volley非常适合一些数据量不大,但需要频繁通信的网络操作 ...
- WKWebView使用过程中的那些坑
问题产生背景: 新开发的页面中有一部分的界面是需要展示后端接口返回的HTML代码,包括文字和图片.所以就自然而然的要使用iOS原生的WebKit. 鉴于Xcode 8发布以后,编译器支持的最低版本(D ...