简体   繁体   中英

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).

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. where you need to match them up based on Cyl = Cylin and Sph = Sphere.

In your example, all the records have the same ID, of 1000. I hope this is not the case. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM