更新一个表的多条记录,用另一个表的数据做一些计算。[英] Update multiple rows of a table with some calculation done with data from another table .

本文是小编为大家收集整理的关于更新一个表的多条记录,用另一个表的数据做一些计算。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有 3 个表,我的要求是更新一个表的几条记录(使用数学公式),并参考另一个表.
样本表及样本数据如下:-

已过滤

ECodeCountyGroupnameProjectnameWorktype持续时间
101NYProj1WT110
101NYProj2WT220
102NPProj1WT120
101NPProj3WT310
102NKProj1WT120



NonProd
ECodeCountyGroupName持续时间
101NY10
102NP10
103NY10



产品
ECodeCountyGroupname持续时间
101NY30
101NP35
102ND35
103纽约40
104GP45


我想根据以下条件更新"已过滤"表列名称为"持续时间":-
更新过滤
Set Duration= (公式为:- Filtered.Duration * NonProd.Duration)/Prod.Duration when
条件一:Filtered.CountyGroupname=NonProd.CountyGroupname=Prod.CountyGroupName
条件二:Filtered.Ecode=NonProd.Ecode=Prod.ECode
存在于哪里
()

注意:- 根据逻辑,"过滤表"中只有前 3 行的持续时间得到更新.其余字段不会更改.

推荐答案

您好,

使用更新连接

http://geekswithblogs.net/faizanahmad/archive/2009/01/05/join-in-sql-update--statement.aspx[^]

希望这会有所帮助.

你可以通过查询来做到这一点,但如果你使用存储过程,那就太糟糕了,因为你必须使用连接和子查询,这会创建非常复杂的查询.

本文地址:https://www.itbaoku.cn/post/1394887.html

问题描述

I have 3 table and my requirement is to update few records of a table(Using mathematical formula) with refence of the another table.
Sample table and sample data are as below:-

Filtered

ECodeCountyGroupnameProjectnameWorktypeDuration
101NYProj1WT110
101NYProj2WT220
102NPProj1WT120
101NPProj3WT310
102NKProj1WT120



NonProd
ECodeCountyGroupNameDuration
101NY10
102NP10
103NY10



Prod
ECodeCountyGroupnameDuration
101NY30
101NP35
102ND35
103NY40
104GP45


I want to update the "Filtered" Table Column Name is "Duration" as per the condition that:-
Update Filtered
Set Duration= (Formula will be:- Filtered.Duration * NonProd.Duration)/Prod.Duration when
Condition 1: Filtered.CountyGroupname=NonProd.CountyGroupname=Prod.CountyGroupName
Condition 2: Filtered.Ecode=NonProd.Ecode=Prod.ECode
where exists
()

Note:- As per the logic only first 3 Rows Of the duration in "Filtered Table" got updated.Rest of the field will not be changed.

推荐答案

Hi,

use update with joins

http://geekswithblogs.net/faizanahmad/archive/2009/01/05/join-in-sql-update--statement.aspx[^]

Hope this helps.

U can do this by query but it is batter if u use stored procedure because u have to use join and sub-query which create query very complex.