2017-12-20 9 views
1

Entfernung habe ich eine Tabelle wie folgt aus:Wählen Sie Abfrage nach relevanten Daten während Duplikate

ID Col_A Col_B Col_C Col_D Col_E Col_F Col_G Col_H 
1 yyy  null NULL NULL NULL NULL NULL 1 
1 yyy  null NULL NULL NULL NULL NULL null 
1 yyy  null NULL NULL NULL NULL NULL 0 
1 yyy  null NULL NULL NULL NULL NULL null 
1 yyy  null NULL NULL NULL NULL NULL 0 
26 null 17,5 NULL NULL NULL NULL NULL null 
26 null 17,5 NULL NULL NULL NULL NULL 1 
26 null 17,5 NULL NULL NULL NULL NULL 0 
26 null 17,5 NULL NULL NULL NULL NULL 0 
26 null 17,5 NULL NULL NULL NULL NULL 1 

ich doppelte IDs entfernen möchten, aber relevante Daten zu halten. Also hier gewünschte Ergebnis wäre:

ID Col_A Col_B Col_C Col_D Col_E Col_F Col_G Col_H 
1 yyy  null NULL NULL NULL NULL NULL 1 
26 null 17,5 NULL NULL NULL NULL NULL 1 

Der Code, den ich versuchte, war:

SELECT ID, 
    MAX (ISNULL (Col_A, 0)) AS Col_A, 
    MAX (ISNULL (Col_B, 0)) AS Col_B, 
    MAX (ISNULL (Col_C, 0)) AS Col_C, 
    MAX (ISNULL (Col_D, 0)) AS Col_D, 
    MAX (ISNULL (Col_E, 0)) AS Col_E, 
    MAX (ISNULL (Col_F, 0)) AS Col_F, 
    MAX (ISNULL (Col_G, 0)) AS Col_G, 
    MAX (ISNULL (Col_H, 0)) AS Col_H 
    FROM TableA 
    GROUP BY ID 
    ORDER BY 1 

Arbeiten an MSSQL 2008

+0

Ich habe den Titel bearbeitet, da Sie eigentlich keine Dubletten aus Ihrer Tabelle entfernen möchten, sondern nur nicht duplizierte Datensätze. –

Antwort

2

Ich würde einfach tun:

select max(col_a) as col_a, 
     max(col_b) as col_b, 
     . . . 
from tablea 
group by id 
order by id; 

Die isnull() ist nicht notwendig.

+0

Danke für Ihre schnelle Antwort. Col_H gibt mir NULL anstelle des gewünschten Wertes 1. (für beide IDs hier) –

+0

Sorry, ich jagte einen Geist. Aus irgendeinem Grund enthält Col_H die Zeichenkette 'Null' anstelle von NULL, wenn keine Daten vorhanden sind. Daher funktionierte der MAX nicht wie erwartet. Deine Antwort war richtig. Danke noch einmal! –

Verwandte Themen