Suppose we have a field ("Flag"). How we can shift records of this flag in another field ("ShiftField") by using SQL code?
See the example:
ID Flag ShiftFlag
1 0
2 1 0
3 1 1
4 1 1
5 0 1
6 0 0
7 1 0
8 1 1
9 1 1
10 0 1
My efforts already was something like below:
for i=1 to n str =
"UPDATE HoboData Set ShiftFlag = Wet_Estimated80 WHERE ID = " & i + 1
next i
Dim cmd As OleDbCommand = New OleDbCommand(str, Connect)
but this way is very slow
Best Answer
This will do the trick in recent versions of SQL Server and Oracle:
If you don't have LAG(), for example in MS SQL Server 2008, you can use ROW_NUMBER() like this:
Tested on SQLFiddle.