Subtracting one row of data from another in SQL

前端 未结 7 747
抹茶落季
抹茶落季 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:37

    edit: fixed when re-read Q (misunderstood)

    SELECT f.id, 
           f2.id, 
           f.length, 
           f2.length, 
           (f.length -f2.length) AS difference
    FROM foo f, 
         foo f2 
    where f2.id = f.id+1
    

    id was ambiguous

    edit: note: tested in mysql 5.0

提交回复
热议问题