简体   繁体   English

如何基于另一列SQL Server 2014中的数据更新一列上的值

[英]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.

相关问题 SQL 服务器 - 根据其他列的值更新一列 - SQL server - Update one column based on the values of other columns 如何根据 SQL Server 中的另一列将一列值转换为两个不同的列值? - How to convert one column values to two different column values based on other column in SQL Server? SQL Server如何基于子查询内部联接上的一列定义其他列值? - SQL Server how to define other column values based on one column on subquery inner join? 如何基于SQL Server中当前表列的值使用与其他表不同的列值 - How to use different column values from other table based on value on current table column in SQL server SQL Server:根据其他2列中的条件更新boolean列 - SQL Server : update boolean column based on conditions from 2 other columns 如何在 Oracle SQL 语法中根据其他表更新列的值 - How to update values of a column based on other table in Oracle SQL syntax 如何使用 SQL(基于时间戳)中的其他值更新表列的值? - How to update values of a column of a table with values from other in SQL (timestamp based)? SQL 脚本根据其他列值更新列值 - SQL script to update column values based on other column values SELECT DISTINCT 在 SQL 服务器 2014 中的一列上 - SELECT DISTINCT on one column in SQL Server 2014 如何在 sql server 2014 中显示列的所有值的集合 - How to show collection of all values of a column in sql server 2014
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM