[英]Match similar not same column in SQL
I have two tables. 我有两张桌子。 Table A as under:
表A如下:
ID | Val1 | Val2 | Val 3
1 55.198 200 67.501
1 68.647 205 149.011
1 150.545 210 250.124
and Table B as under: 表B如下:
ID_From_TableA | Val1 | Val2
1 55.199 200
1 56.156 200
1 68.647 205
Table A has unique value if we combine 如果我们结合使用,表A具有唯一的价值
ID and Val2
ID和Val2
and 和
I want to update column Val2 of tableB with Val2 of TableA when, TableA.ID = Table2.ID_From_TableA and TableA.Val1 is approximately equal to TableB.Val1 当TableA.ID = Table2.ID_From_TableA和TableA.Val1大约等于TableB.Val1时,我想用TableA的Val2更新tableB的Val2列
I tried below given code but it didn't work 我在给定的代码下尝试了一下,但是没有用
select B.ID, B.Val1
B.Val2 = A.VAL1
from TableA A
left join TableB B
on A.ID_From_TableA = B.ID
where (B.VAL1-A.Val1)>1
also I tried, but didnt work 我也尝试过,但是没有工作
select B.ID, B.Val1
B.Val2 = A.VAL1
from TableA A
left join TableB B
on A.ID_From_TableA = B.ID
where B.VAL1 like A.Val1
Can someone please help? 有人可以帮忙吗?
PS - I am using MSSQL PS-我正在使用MSSQL
Use an update join, and make sure that TableB
appears on the left side of that join: 使用更新
TableB
,并确保TableB
出现在该TableB
的左侧:
UPDATE b
SET b.Val2 = a.Val2
FROM tableB b
INNER JOIN tableA a
ON b.ID = a.ID AND ABS(b.Val1 - a.Val1) < 0.01;
This assumes a tolerance of less than 0.01 difference between Val1
values as implying equality. 假设
Val1
值之间的差异小于0.01,则表示相等。 You may tune this tolerance to your needs. 您可以根据需要调整此公差。
Thanks Tim, 谢谢蒂姆,
I tried below code and it worked like a charm.. 我尝试下面的代码,它就像一个魅力..
update
TableB
set
Val2 = a.Val2
from TableB b
inner join TableA a
on a.ID = bookmarking.ID and ABS(bookmarking.Val1) between a.Val2 and a.Val2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.