如何把SQL表中的第一行資料更新到第二行中

時間 2021-10-14 22:25:42

1樓:du瓶邪

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,如果沒有,給個排序的欄位也可!

2樓:匿名使用者

select a,b,c,lag(c) over(order by a) as aa from t_a;

這樣,aa就是你要的值了,然後做一個更新語句。

3樓:

-- m$sql:

update a set a=b.c, c=b.c-a.bfrom t_a as a

join t_a as b on a.id + 1 = b.id

4樓:匿名使用者

create table #temp1

([id] [int] identity(1,1) not null,

[a] [int] null,

[b] [int] null,

[c] [int] null

)insert into #temp1 (a,b,c) values(10,3,7)

declare @i int

set @i = 1

while @i < 30

begin

insert into #temp1 (a,b,c)select c,b,c-b from #temp1 where id = @i

set @i = @i+1

endselect * from #temp1

5樓:匿名使用者

可以這樣

update t_a

set a = (select a from t_a where id = 1) - isnull((select sum(b) from t_a b where b.id < t_a.id),0),

c = (select a from t_a where id = 1) - (select sum(b) from t_a b where b.id <= t_a.id)

各位老師:請問如何在sql中取得一個表第二行的資料?

6樓:匿名使用者

select top 2 *

from test

where id not in (select top 1 id from test)

7樓:

在sql表中沒有記錄先後之分,哪一行叫第二行呢?

用vb判斷第二行資料與第一行資料是否相同

歸萱 private sub command1 click dim a 1 to 4,1 to 4 i,j,s,n as single a 1,1 2008222001 a 1,2 大學化學 a 1,3 70 a 1,4 1 a 2,1 2008222001 a 2,2 大學物理 a 2,3 60 ...

Excel如何通過VBA找出最後一行資料所在的列

陽光上的橋 一般有兩個方法,一是使用activesheet.usedrange,這個表示所有已經使用的區域,一般可以賦值給陣列完成所有資料的讀取,也可以使用activesheet.usedrange.rows.count和activesheet.usedrange.columns.count獲得區域...

excel中如何將一行資料中的最大值和最小值突出顯示

這個需要使用 條件格式 功能,來突出顯示,選擇第一個資料,如a1然後,條件格式 新建規則 使用公式 參考公式如下 a1 max a1 r1 以上假設資料是從a列到r列 設定好要突出顯示的效果就可以突出顯示最大值,然後用格式刷將a1的格式刷到全行即可完成最大值提醒,同樣方法設定最小值,參考公式 a1 ...