Create col of two rows subtracted in query

Hello,

I have a table with cols grand total and GPM

I'd like to add a col in-between. Col B = Col A Row B - Col A Row A and keep that pattern sequentially down the column for the table.

I get the basic SQL query, but am unsure of how to keep that pattern sequentially turning out to the table.

Any ideas or tips in the right direction would be much appreciated.

Given the following table called totalizer (I'm really hip with names):
image

Use of a LAG function should do it.

SELECT total,
       total - LAG(total) OVER (ORDER BY ndx) AS difference,
       gpm
FROM totalizer

image

4 Likes

That’s perfect! Thank you !