sqlserver用当前行减去前一行的数据的简单处理方法 -- 不使用循环

;with cet
-- 1.递归取出需要的数据
as 
(
-- 对于CET通用表达式增加自定义列的问题?-- 附带说明
-- 自定义列必须满足下面二者中的一种,否则报错:[在递归查询 "cet" 的列 "CommType" 中,定位点类型和递归部分的类型不匹配。]
-- a.自定义列CommType显示转换
-- b.或者在使用自定义列时,前面列的长度必须比后面列的长度大
select *,CONVERT(varchar(10),'CA') as 'CommType' from TB_Biz_Account where Account = 10001
union all
select Acc.*,'CAD') as 'CommType' from TB_Biz_Account Acc inner join cet on Acc.Account = cet.IBAccount
where Acc.Account > 0

)


-- 2.添加序列
select IDENTITY(int,1,1) as 'ID',*
into #tmpAcc
from cet 

where AccountType >= 3 and Account > 1000


-- 3.当前行的Policy 减去 上一行的Policy
select mm.*,
 isnull(mm.Policy,0) - isnull(nn.Policy,0)
from #tmpAcc mm
left join #tmpAcc nn on mm.ID = nn.ID + 1
where isnull(mm.Policy,0) > 0


drop table #tmpAcc

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...