Home > database > How to calculate time of adjacent data subtraction
How to calculate time of adjacent data subtraction
Time:09-25
How to calculate time of adjacent data presupposed, 29 RM_value column equals 29 column x_value minus 28 column x_value, 28 RM_value column equals 28 same column x_value x_value minus 27 column, a great god please help
CodePudding user response:
The test data
-If not object_id (N 'Tempdb for.. # T ') is null Drop table # T Go The Create table # T (Effective_Date DATETIME, [X_value] a decimal (18, 8)) Insert # T Select N '2019-08-27 01:00:20. 000, 1.00 union all Select N '2019-08-29 09:18:44. 000, 2.50 union all Select N '2019-08-29 09:20:33. 000, 3.00 Go - the end of the test data ; WITH cte AS ( Select *, ROW_NUMBER () OVER (ORDER BY Effective_Date) rn from # T ) SELECT a. *, a.X _value - ISNULL (b.X _value, 0) AS RM_value FROM cte a LEFT JOIN cte ON a.r n=b b.r n + 1