SQL交错相减

TableInfo表中有ID,time,val 3个字段,


现需要按时间倒叙 展示以下4个字段  时间,当前val值,下一条val值,二者差值。

SQL交错相减_第1张图片

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,交错相减)