Alejandro Lee
Alejandro Lee

Reputation: 167

SQL - How to offset a table by 1 row

How would I join a table onto itself and offset the second table to shift up by 1 row?

I want to do this in order to calculate the amount of days until the next sale date.

Upvotes: 1

Views: 424

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

If you have data recording sales, then you would get the next date using lead():

select s.*,
       lead(saledate) over (partition by customerid order by saledate) as next_saledate
from sales s;

Upvotes: 2

Related Questions