Subtracting one row of data from another in SQL

前端 未结 7 741
抹茶落季
抹茶落季 2020-12-09 19:06

I\'ve been stumped with some SQL where I\'ve got several rows of data, and I want to subtract a row from the previous row and have it repeat all the way down.

So her

相关标签:
7条回答
  • 2020-12-09 19:43

    This might help you (somewhat).

    
    select a.id, a.length, 
    coalesce(a.length - 
        (select b.length from foo b where b.id = a.id + 1), a.length) as diff
    from foo a
    
    
    0 讨论(0)
提交回复
热议问题