SQL Query根据同一个表中其他列的值更新列

时间:2011-12-01 22:29:39

标签: sql sql-server sql-server-2008

好的,这很难说,所以这里......

我正在使用MS SQL Server 2008 R2。我有一个临时表,可以说有两个已经填充的列。我想根据前两列的值填充第三个空列。我想要做的是为col1和col2的每个匹配组合创建一个guid(使用NEWUID())。这是一个直观的例子:

假设我最初有一个临时表:

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

我希望它能够使用新的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

注意如何为每个匹配对创建新的GUID。因此,James / Running组合对于所有James / Running组合具有相同的GUID ...而John / Running也具有与John / Running组合相同的GUID,但与James / Running组合的GUID不同。 / p>

我试图尽可能清楚地表达这一点,但希望这不是很清楚!

有人可以告诉我,为了使用正确的GUID更新临时表,SQL查询会是什么样子?

提前致谢。

赖安

4 个答案:

答案 0 :(得分:3)

使用NEWID()seems to be a pain。使用它,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 

可生产

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

答案 1 :(得分:0)

我确定有更好的方法可以做到这一点,但您可以尝试以下方法:

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

答案 2 :(得分:0)

嗯,我知道你没有使用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;

看起来这样可以在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;

虽然表现不佳。

答案 3 :(得分:0)

我同意@ConradFrix首先使用NEWID(),Lamak脚本应按如下所述进行修改。对我来说很好。谢谢大家。

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