How to get previous row data in sql server

后端 未结 4 1093
青春惊慌失措
青春惊慌失措 2021-01-20 23:53

I would like to get the data from previous row. I have used LAG function but did not get the expected result.

Table:-

col1  col2  col3
ABCD    1   Y
ABCD         


        
4条回答
  •  再見小時候
    2021-01-21 00:35

    If you are on 2008 or earlier, try this:

        select t1.col1, t1.col2, t1.col3, t2.col3 as col4
        from table1 t1
        left join table1 t2 on t1.col1 = t2.col1 and t1.col2 - 1 = t2.col2
    

    the lag() function is the bee's knees, though. Use that, if you can.

提交回复
热议问题