hello11
hello11

Reputation: 105

Multiple day on day changes based on dates in data as not continuous

See table A. There are number of sales per date. The dates are not continuous. I want table B where it gives the sales moves per the previous date in the dataset.

I am trying to do it in SQL but get stuck. I can do an individual day on day difference by entering the date but I want one where I don't need to enter the dates manually

A

Date         Sales
01/01/2019      100
05/01/2019      200
12/01/2019      50
25/01/2019      25
31/01/2019      200

B

Date            DOD Move
01/01/2019      -
05/01/2019      +100
12/01/2019      -150
25/01/2019      -25
31/01/2019      +175

Upvotes: 2

Views: 31

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269953

Use lag():

select t.*,
       (sales - lag(sales) over (order by date)) as dod_move
from t;

Upvotes: 3

Related Questions