![](/img/trans.png)
[英]SQL Query to update a column based on the values of other columns in the same table
[英]SQL update based on values in same table
我試圖將這兩個查詢放在一起,但我不確定如何。 我正在嘗試將去年版本的相同預算表中的值復制到今年版本,基於它們具有相同的帳戶和工作訂單。
update tablename
set depreciation = x.depreciation,
profile = x.profile,
description = x.description
where version = '2019OP'
and account = x.account
and workorder = x.workorder
(select * from tablename where version = '2018OP' and batch = '') x
我在這個論壇上發現了與此類似的其他問題,但我找不到適合我的答案。
謝謝
編輯 - 這是 SQL Server 2012,兼容級別是 SQL Server 2008(100) 在 SSMS 中使用 SQL
像這樣的東西
update tablename from tablename
set depreciation = x.depreciation,
profile = x.profile,
description = x.description
inner join tablename x on x.version = '2019OP'
and tablename.account = x.account
and tablename .workorder = x.workorder
您沒有指定 RDBMS。 在 Sql Server 中,這應該可以工作...
UPDATE x1
SET x1.depreciation = x.depreciation,
x1.profile = x.profile,
x1.description = x.description
FROM tablename x1
INNER JOIN tablename x ON x1.account = x.account AND x1.workorder = x.workorder
WHERE x1.version = '2019OP'
AND x.version = '2018OP' AND x.batch = ''
update tn set tn.depreciation = ly.depreciation, tn.profile = ly.profile, tn.description = ly.description
FROM tablename as tn INNER JOIN
(
SELECT
account ,
workorder,
depreciation,
profile ,
description
FROM tablename AS
where version = '2018OP' and batch = ''
) AS ly ON ly.account = tn.account AND ly.workorder = tn.workorder
tn.version = '2019OP'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.