M
M
Max Trophy2016-09-09 15:58:38
Transact SQL
Max Trophy, 2016-09-09 15:58:38

T-SQL - get previous value How?

Hey!
There is a model field. How to write a code in t-sql, what function can be used to get the values ​​​​as in the parent_model field? That is, for each value of model, its parent value is needed.
473671b807ea48f3bc0d7021e4c07e73.PNG
thanks for any hints.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sokolov, 2016-09-09
@jimquery

Use window functions LAG, LEAD:

SELECT model, LAG(model, 1,0) OVER (ORDER BY model ) as parent_model
FROM  [mytable]

1 - offset back,
0 - default value for the first value.
These options can be omitted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question