[英]How to update the values on one column based on the data from other column SQL Server 2014
I would like to update the values on column based on the information from other column in SQL Server 2014. The table format is as follows (note: there are no null values). 我想根据SQL Server 2014中其他列的信息更新列上的值。表格式如下(注意:没有空值)。
First scenario: 第一种情况:
ID Name Values TimeStamp
---- ------------------ ------ ---------
1000 ARMS Cyl L RU 10 12:00
1000 ARMS Cyl R RU 20 12:00
1000 ARMS Sph L RU 30 12:00
1000 ARMS Sph R RU 40 12:00
1000 HANDS Cylin L RU 50 12:00
1000 HANDS Cylin R RU 60 12:00
1000 HANDS Sphere L RU 70 12:00
1000 HANDS Sphere R RU 80 12:00
Expected output: 预期产量:
ID Name Values TimeStamp
---- ------------------ ------ ---------
1000 ARMS Cyl L RU 10 12:00
1000 ARMS Cyl R RU 20 12:00
1000 ARMS Sph L RU 30 12:00
1000 ARMS Sph R RU 40 12:00
1000 HANDS Cylin L RU 10 12:00
1000 HANDS Cylin R RU 20 12:00
1000 HANDS Sphere L RU 30 12:00
1000 HANDS Sphere R RU 40 12:00
Second scenario: 第二种情况:
ID Name Values TimeStamp
---- ------------------ ------ ---------
1000 ARMS Cyl L RU 10 12:00
1000 ARMS Cyl R RU 20 12:00
1000 HANDS Cylin L RU 50 12:00
1000 HANDS Cylin R RU 60 12:00
1000 HANDS Sphere L RU 70 12:00
1000 HANDS Sphere R RU 80 12:00
Expected output: 预期产量:
ID Name Values TimeStamp
---- ------------------ ------ ---------
1000 ARMS Cyl L RU 10 12:00
1000 ARMS Cyl R RU 20 12:00
1000 HANDS Cylin L RU 10 12:00
1000 HANDS Cylin R RU 20 12:00
1000 HANDS Sphere L RU 70 12:00
1000 HANDS Sphere R RU 80 12:00
The logic of the code is as follow(this is just for explanation), I need to show the results on the database query as shown in the expected output: 代码的逻辑如下(这仅用于说明),我需要在数据库查询中显示结果,如预期输出所示:
if('ARMS Cyl L RU' = '')
{
/*take value from 'HANDS Cylin L RU'*/
}
else
{
/*the value is empty*/
}
/* continue on for the next 3 values for ARMS Cyl R RU, ARMS Sph L RU, ARMS Sph R RU*/
I need to write a query to show the expected outcome. 我需要编写查询以显示预期的结果。 Any help would be appreciated!
任何帮助,将不胜感激!
If I understand your requirements, you want to update the value in the field Values, to reflect the number for the ARMS products, based on the matching HANDS products. 如果我了解您的要求,您想根据匹配的HANDS产品更新值字段中的值,以反映ARMS产品的编号。 where you need to match them up based on Cyl = Cylin and Sph = Sphere.
您需要根据Cyl = Cylin和Sph = Sphere进行匹配。
In your example, all the records have the same ID, of 1000. I hope this is not the case. 在您的示例中,所有记录具有相同的ID(即1000)。我希望情况并非如此。 It also looks like you should try to standardize your inventory item list, and only then worry about the numbers.
看起来您应该尝试标准化库存项目清单,然后才担心数量。 (suggestion: create a new table with two columns: one - the current names, and the second - the desired column in your new table, and keep going from there).
(建议:创建一个具有两列的新表:一列-当前名称,第二列-新表中所需的列,并从那里继续前进)。
In the meanwhile, I think this code addresses your specific request: 同时,我认为这段代码可以满足您的特定要求:
with myTable as (
select
[Name] as OriginalName,
replace(
replace(
replace(
[Name],
'HANDS','ARMS'),
'Cylin','Cyl'),
'Sphere','Sph') as StandardName,
[Values]
from YOURTABLENAMEHERE
)
update t0
set [Values] = t1.[Values]
from
myTable t0
inner join
myTable t1
on (t0.StandardName = t1.StandardName and t0.OriginalName <> t1.OriginalName)
where
t0.OriginalName like 'HANDS%'
I think you need to use merge for that. 我认为您需要为此使用合并。 Here is my query for that.
这是我的查询。 I wish it will solve your problem.
希望它能解决您的问题。
MERGE INTO first_scenario f
USING second_senario s
ON (f.name=s.name)
WHEN MATCHED THEN
UPDATE SET f.id=s.id,
f.name=s.name,
f.values=s.values,
f.timestamp=s.timestamp
WHEN NOT MATCHED THEN INSERT
VALUES (select s.id,
s.name,
s.values,
s.timestamp from second_scenario);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.