3
How to create a column with the value of the previous row of another column?
Follow example, I have the column time and I want to create the column time_lag
:
Tabela inicial:
id | title | time | domain
32 title1 12:30 domain1
33 title1 13:30 domain2
34 title2 14:20 domain1
35 title2 14:50 domain2
36 title3 15:30 domain5
Expected result:
id | title | time | domain |time_lag
32 title1 12:30 domain1
33 title1 13:30 domain2 12:30
34 title2 14:20 domain1 13:30
35 title2 14:50 domain2 14:20
36 title3 15:30 domain5 14:50
Do you need to do it permanently on the table? Wouldn’t a wish with that result be enough?
– Sergio
@Sergio, I need this column permanently.
– Henrique Pizarro
But if you don’t want to touch the table, there is the lag function, which can be much more useful: http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/
– Renato Afonso
@Henriquepizarro, which database and version you are using ?
– Marco Souza