1

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.

Alejandro Lee
  • 127
  • 1
  • 3
  • 14
  • 2
    Edit your question and provide sample data and desired results. – Gordon Linoff Mar 16 '16 at 00:45
  • What you're probably looking for is a [self-join](https://stackoverflow.com/questions/2458519/explanation-of-self-joins). We could answer better if you'd provide a sample of your table data, the results you'd like, and what you've tried. – Schwern Mar 16 '16 at 00:47

1 Answers1

2

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;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786