本文是小编为大家收集整理的关于更新一个表的多条记录,用另一个表的数据做一些计算。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我有 3 个表,我的要求是更新一个表的几条记录(使用数学公式),并参考另一个表.
样本表及样本数据如下:-
已过滤
ECode | CountyGroupname | Projectname | Worktype | 持续时间 |
101 | NY | Proj1 | WT1 | 10 |
101 | NY | Proj2 | WT2 | 20 |
102 | NP | Proj1 | WT1 | 20 |
101 | NP | Proj3 | WT3 | 10 |
102 | NK | Proj1 | WT1 | 20 |
NonProd
ECode | CountyGroupName | 持续时间 |
101 | NY | 10 |
102 | NP | 10 |
103 | NY | 10 |
产品
ECode | CountyGroupname | 持续时间 |
101 | NY | 30 |
101 | NP | 35 |
102 | ND | 35 |
103 | 纽约 | 40 |
104 | GP | 45 |
我想根据以下条件更新"已过滤"表列名称为"持续时间":-
更新过滤
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[^]
希望这会有所帮助.
你可以通过查询来做到这一点,但如果你使用存储过程,那就太糟糕了,因为你必须使用连接和子查询,这会创建非常复杂的查询.
问题描述
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
ECode | CountyGroupname | Projectname | Worktype | Duration |
101 | NY | Proj1 | WT1 | 10 |
101 | NY | Proj2 | WT2 | 20 |
102 | NP | Proj1 | WT1 | 20 |
101 | NP | Proj3 | WT3 | 10 |
102 | NK | Proj1 | WT1 | 20 |
NonProd
ECode | CountyGroupName | Duration |
101 | NY | 10 |
102 | NP | 10 |
103 | NY | 10 |
Prod
ECode | CountyGroupname | Duration |
101 | NY | 30 |
101 | NP | 35 |
102 | ND | 35 |
103 | NY | 40 |
104 | GP | 45 |
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.