怎么让sql表的第二行取第一行数据的值

在建视图的时候第二行的firstBalance取第一行的lastBalance的值,以此类推

Create Table #TMP

(

ID int identity(1,1),

firstBalance int,

lastBalance int


Insert Into #TMP (lastBalance) Values(1)

Insert Into #TMP (lastBalance) Values(3)

Insert Into #TMP (lastBalance) Values(5)

Insert Into #TMP (lastBalance) Values(2)

Insert Into #TMP (lastBalance) Values(9)

Insert Into #TMP (lastBalance) Values(10)


Select  (Select lastBalance From #Tmp C Where ID=(Select Max(ID) From #Tmp A Where A.ID<#TMP.ID)) As firstBalance ,lastBalance From #TMP

 

Drop Table #TMP

--表需要一个自增的ID,如果没有,给个排序的字段也可



温馨提示:答案为网友推荐,仅供参考