在Db2中如何计算相邻行的差异

时间:2012-01-29 13:03:13

标签: sql db2

我想将范围计算为row1-row2,在db2查询中需要帮助以获取范围列

name,age,range   
a,40,0     
b,20,20  
c,7,13  
d,5,2  
e,1,4  

范围列计算

first value is 0  
(40-20)  
(20-7)  
(7-5)  
(5-1)  

1 个答案:

答案 0 :(得分:2)

SELECT name, 
       age,
       (lag(age, 1, age) over (order by age desc)) - age as agediff
FROM your_table
ORDER BY age desc