2012-03-30 14 views
0

Ich habe eine Tabelle mit den Feldern Id1, ID2, Points1, Points2. Ich habe eine andere Tabelle mit den Spalten Id1, Id2, Address1, address2, city 1, city2, state1, state2. Ich muss die pointsA und PointsB Spalte aktualisieren, basierend darauf, wie viele der Spalten in der zweiten Tabelle gefüllt sind, was bedeutet, dass sie nicht null haben. ExAktualisieren Sie eine Spalte basierend auf Zähler aus einer anderen Tabelle?

Tabelle 1

ID1 ID2 Points 1 Points2 
1  2  2   0 

Tabelle 2

ID1 ID2 Address1 address2 city 1 city2 state1 state2. 
1 2 a1  null  null null s1  null 

Wie kann ich dies in SQL Server 2008 erreichen?

Dank

Antwort

1
UPDATE t1 
SET 
    Points1 = t2.Points1, 
    Points2 = t2.Points2 
FROM 
    Table1 t1 JOIN (
    SELECT 
     ID1, 
     ID2, 
     CASE WHEN Address1 IS NULL THEN 0 ELSE 1 END + 
     CASE WHEN City1 IS NULL THEN 0 ELSE 1 END + 
     CASE WHEN State1 IS NULL THEN 0 ELSE 1 END AS Points1, 
     CASE WHEN Address2 IS NULL THEN 0 ELSE 1 END + 
     CASE WHEN City2 IS NULL THEN 0 ELSE 1 END + 
     CASE WHEN State2 IS NULL THEN 0 ELSE 1 END AS Points2, 
    FROM Table2 
) t2 ON t1.ID1 = t2.ID1 AND t1.ID2 = t2.ID2 
+0

Dank lot..That für mich CHEERS funktioniert !! – user1304271

Verwandte Themen