marcin2x4
marcin2x4

Reputation: 1449

Move value from row below to adjacent column in SQL

I would need some help with below case: My data contains dates of order arrival in one column. enter image description here

I want to add second column what would use date from one row below: enter image description here

Upvotes: 1

Views: 342

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270873

Use lead():

select t.dept, t.date as arrival, t.next_arrival
from (select t.*, lead(date) over (partition by dept order by date) as next_arrival
      from t
     ) t
where t.next_arrival is not null;

If you are happy with all rows in the result set and the next_arrival being NULL, then you don't need the subquery.

Upvotes: 1

Related Questions