2011-12-01 12 views
5

Ok questo è difficile da frase, quindi ecco qui ...query SQL per aggiornare una colonna in base ai valori di altre colonne nella stessa tabella

sto utilizzando MS SQL Server 2008 R2. Ho una tabella temporanea che consente di avere due colonne già popolate. C'è una terza colonna vuota che voglio popolare in base al valore delle prime due colonne. Quello che voglio fare è creare un guid (usando NEWUID()) per ogni combinazione corrispondente di col1 e col2. Ecco un esempio visivo:

Diciamo che ho una tabella temporanea che assomiglia a questo inizialmente:

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

voglio che si aggiorna con nuovi GUID in modo che assomiglia a questo:

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 

Si noti come viene creato un nuovo GUID per ciascuna coppia corrispondente. Quindi la combinazione James/Running ha lo stesso GUID per tutte le combo di James/Running ... e John/Running ha anche lo stesso GUID per le combo di John/Running, ma non lo stesso GUID delle combo di James/Running.

Ho cercato di renderlo il più chiaro possibile, ma spero che non sia chiaro come fango!

Qualcuno può mostrarmi quale sarebbe la query SQL per aggiornare quella tabella temporanea con i GUID corretti?

Grazie in anticipo.

Ryan

+0

Che database stai usando? –

+0

Concat entrambi i campi non è una soluzione? – danihp

+0

Sto usando SQL Server 2008, post appena aggiornato. – Ryan

risposta

3

Utilizzando NEWID() seems to be a pain. Usarlo come CTE crea un ID sperato, quindi hai bisogno di una tabella intermedia.

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 

Produce

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 
0

Sono sicuro che ci sono modi migliori per fare questo, ma si può provare il seguente:

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 
+0

Prendo il fatto che non hai provato quello –

+0

@ConradFrix - E avresti assolutamente ragione. Sono effettivamente su un PC senza RDBMS. – Lamak

+0

Inizialmente ho provato la stessa cosa e non ha funzionato. Vedi la query data.se [Sample for lamak] (http://data.stackexchange.com/stackoverflow/s/2212/sample-for-lamak) –

0

Beh, so che non si sta usando mySQL, ma questo è come avrebbe funzionato in MySQL (testato)

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; 

Ecco come si presenta funzionerebbe in SQL 2008 (non testato)

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; 

Le prestazioni non sarebbero comunque buone.

Problemi correlati