本文主要是介绍SQL交错相减,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
TableInfo表中有ID,time,val 3个字段,
现需要按时间倒叙 展示以下4个字段 时间,当前val值,下一条val值,二者差值。
declare @PageSize int=10,@PageIndex int=1
select * from (
select top 1000 ROW_NUMBER() over(order by newTable.ID desc) as mn,newTable.rn,newTable.id,newTable.time,newTable.val,oldTable.val as preVal,CONVERT(money,newTable.val)-CONVERT(money, oldTable.val) as Amplitude from (select top 1000 ROW_NUMBER() over(order by ID) as rn, id,time,val from TableInfo order by id desc) as newTable,(select top 1000 ROW_NUMBER() over(order by ID) as rn, id,time,val from TableInfo order by id desc) as oldTable where newTable.rn=oldTable.rn+1
) as mmt
where mmt.mn between @PageSize*(@PageIndex-1)+1 and @PageSize*@PageIndex
这篇关于SQL交错相减的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!