简体   繁体   English

SQL查询根据同一表中其他列的值更新列

[英]SQL Query to update a column based on the values of other columns in the same table

Ok this is difficult to phrase, so here goes...好吧,这很难说,所以这里是......

I am using MS SQL Server 2008 R2.我正在使用 MS SQL Server 2008 R2。 I have a temp table that lets say has two already populated columns.我有一个临时表,可以说已经填充了两个列。 There is a third empty column I want to populate based on the value of the first two columns.我想根据前两列的值填充第三个空列。 What I want to do is create a guid (using NEWUID()) for each matching combo of col1 and col2.我想要做的是为 col1 和 col2 的每个匹配组合创建一个 guid(使用 NEWUID())。 Here is a visual example:这是一个视觉示例:

Lets say I have a temp table that looks like this initially:假设我有一个最初看起来像这样的临时表:

Name    Activity    SpecialId
James   Running     
James   Running
James   Walking
John    Running
John    Running
John    Walking

I want it to get updated with new GUIDs so that it looks like this:我希望它使用新的 GUID 进行更新,使其看起来像这样:

Name    Activity    SpecialId
James   Running     SOMEFAKEGUID_1
James   Running     SOMEFAKEGUID_1
James   Walking     SOMEFAKEGUID_2
John    Running     SOMEFAKEGUID_3
John    Running     SOMEFAKEGUID_3
John    Walking     SOMEFAKEGUID_4

Notice how a new GUID is created for each matching pair.请注意如何为每个匹配对创建新的 GUID。 So the James/Running combo has the same GUID for all James/Running combos... and the John/Running also has the same GUID for the John/Running combos, but not the same GUID as the James/Running combos do.因此,James/Running 组合对所有 James/Running 组合具有相同的 GUID……而 John/Running 对 John/Running 组合也具有相同的 GUID,但与 James/Running 组合的 GUID 不同。

I tried to make that as clear as possible, but hopefully that isn't clear as mud!我试图尽可能清楚地说明这一点,但希望这不像泥巴那么清楚!

Can someone show me what the SQL query would look like in order to update that temp table with the correct GUIDs?有人可以告诉我 SQL 查询是什么样的,以便使用正确的 GUID 更新该临时表吗?

Thanks in advance.提前致谢。

Ryan瑞安

Using NEWID() seems to be a pain .使用 NEWID() 似乎很痛苦 Using it a CTE creates a sperate ID, so you need some intermediary table instead.使用它 CTE 创建一个单独的 ID,所以你需要一些中间表。

Declare @Table as table (name varchar(20), activity varchar(20) , SpecialID uniqueidentifier)
Declare @DistinctTable as table (name varchar(20), activity varchar(20) , SpecialID uniqueidentifier)

INSERT INTO @Table 
(name, activity)
values
('James','Running'),    
('James','Running'),
('James','Walking'),
('John','Running'),
('John','Running'),
('John','Walking')



WITH distinctt 
     AS (SELECT DISTINCT name, 
                         activity 
         FROM   @Table) 
INSERT INTO @DistinctTable 
SELECT name, 
       activity, 
       Newid() 
FROM   distinctt 

UPDATE @Table 
SET    specialid = dt.specialid 
FROM   @Table t 
       INNER JOIN @DistinctTable dt 
         ON t.activity = dt.activity 
            AND t.name = dt.name 

SELECT * FROM @Table 

Produces生产

name                 activity             SpecialID
-------------------- -------------------- ------------------------------------
James                Running              AAA22BC5-51FE-43B3-8CC9-4C4A5B4CC981
James                Running              AAA22BC5-51FE-43B3-8CC9-4C4A5B4CC981
James                Walking              1722B76B-5F17-4931-8D7C-2ECADB5A4DFD
John                 Running              FBC1F86B-592D-4D30-ACB3-80DA26B00900
John                 Running              FBC1F86B-592D-4D30-ACB3-80DA26B00900
John                 Walking              84282844-AAFD-45CA-9218-F7933E5102C6

I agree with @ConradFrix to use NEWID() first, Lamak script should be modified as mentioned below.我同意@ConradFrix 首先使用NEWID() ,Lamak 脚本应如下所述修改。 It's working fine for me.它对我来说很好用。 Thanks for all.谢谢大家。

WITH TableId AS
(
    SELECT DISTINCT Name, Activity, NEWID() SpecialId
    FROM YourTable
)
UPDATE A
SET A.SpecialId = B.SpecialId
FROM YourTable A
LEFT JOIN (SELECT Name, Activity, SpecialId FROM TableId) B
ON A.Name = B.Name AND A.Activity = B.Activity

I'm sure there are better ways to do this, but you can try the following:我相信有更好的方法可以做到这一点,但您可以尝试以下方法:

WITH TableId AS
(
    SELECT DISTINCT Name, Activity
    FROM YourTable
)

UPDATE A
SET A.SpecialId = B.SpecialId
FROM YourTable A
INNER JOIN (SELECT Name, Activity, NEWID() SpecialId FROM TableId) B
ON A.Name = B.Name AND A.Activity = B.Activity

Well, I know that you're not using mySQL, but this is how it would work in mySQL (tested)好吧,我知道您没有使用 mySQL,但这就是它在 mySQL 中的工作方式(已测试)

update temp_table, (
select uuid() as spec_key, name, activity from (
select distinct name, activity from temp_table) as anotherTemp) as anotheranotherTemp
set specialID = anotheranotherTemp.spec_key
where temp_table.Activity = anotheranotherTemp.activity 
and temp_table.Name = anotheranotherTemp.name;

It LOOKS like this would work in SQL 2008 (not-tested)看起来这可以在 SQL 2008 中工作(未经测试)

MERGE INTO temp_table AS tt
  USING      (
    select newId() as spec_key, name, activity from (
    select distinct name, activity from temp_table) as anotherTemp 
      ON   anotherTemp.activity       = tt.activity
      and anotherTemp.name = tt.name
WHEN MATCHED
  THEN UPDATE
    SET        specialID = anotherTemp.spec_key;

Performance would not be good though.不过性能不会好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据同一表中其他列的不同值更新sql表中的列 - Update column in sql table based on distinct values of other columns in same table 如何根据其他列中的值(对于同一行)更新表的所有行中的列? - How do I update a column in all rows of a table based on values in other columns (for the same row)? 如何在 SQL 中添加一列并在单个查询中同时基于其他列设置其值? - How can i add a column in SQL and set its values based on other columns at the same time in a single query? SQL查询基于表中其他列数据的列更新 - SQL Query for an update of a column based on other column's data in a Table SQL 服务器 - 根据其他列的值更新一列 - SQL server - Update one column based on the values of other columns 根据同一个表中其他列的值为列分配值 - Assign value to a column based on values of other columns in the same table 根据 SQL Server 中其他表中的值更新多个列 - Update multiple columns based on values from other table in SQL Server SQL Update使用同一表中其他列的值 - SQL Update using values from other columns in same table 查询以根据同一表中其他列的值更新数据 - Query to update the data based on value of some other column in same table 基于同一表中的多个列和记录创建SQL更新查询 - Creating a SQL update query based on multiple columns and records in same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM