-3

I have following columns in a table

DATE_TIME , MESSAGE , VALUE , SOURCE , LOT_NAME 

Value is associated with SOURCE, LOT_NAME and MESSAGE.

I want to return data with following columns

DATE_TIME , MESSAGE , NEW_VALUE ,OLD_VALUE , SOURCE , LOT_NAME 

where new value is the current row value and OLD VALUE is value of same MESSAGE same source but immediate previous batch vale.

How do I achieve this? Please kindly help .

NA

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MEngineer
  • 33
  • 6
  • 2
    Is this what you're looking for? https://blog.sqlauthority.com/2013/09/22/sql-server-how-to-access-the-previous-row-and-next-row-value-in-select-statement/ – Robert Harvey Jul 05 '18 at 19:42
  • You really need to provide some sample data and expected result. Maybe your looking for LAG LEAD functions – SqlKindaGuy Jul 05 '18 at 19:49

1 Answers1

0

You are looking for lag():

select t.*,
       lag(value) over (partition by source, lot_name, message order by date_time) as prev_value
from t;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786