Looking for changes from previous rows

Database

Looking for changes from previous rows

Looking for a way to find values of a row only when it's changed from the previous row. What I have are the follow:

SELECT Account,
time_stamp,
Location,

and what I want to do is look for only values where the location has changed and end up with the following:

SELECT Account,
time_stamp,
Location, previous_location

I know I'm missing something easy but everything I try seems to come back wrong.

2 REPLIES
Senior Apprentice

Re: Looking for changes from previous rows

You need the LAG function which is not implemented in Teradata, but easy to rewrite:

SELECT Account,
time_stamp,
Location,
MIN(location)
OVER (PARTITION BY Account -- ? probably
ORDER BY time_stamp
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as previous_location

Re: Looking for changes from previous rows

Thanks for the help Dieter!