2016-05-26 9 views
2

Ich habe Daten wie diese Ich möchte nur die Zeilen in Spalte, wie Zeilen in Spalten mit Komma umwandeln getrennt

DECLARE @Table1 TABLE 
    (id int, name varchar(20), val varchar(20)) 
; 

INSERT INTO @Table1 
    (id , name , val) 
VALUES 
    (222, 'ram', 'match'), 
    (222, 'ra', 'nomatch'), 
    (222, 'man', 'nomatch'), 
    (222, 'kim', 'match') 
; 

Ich brauche eine Ausgabe wie diese

ID Match  NoMatch 
222 ram,kim ra,man 

unten Daten konvertieren Eigentlich habe ich Pivot und Max Condition ausprobiert wo mir Point fehlt.

+1

diese beiden Links sollen Ihnen helfen, http://stackoverflow.com/questions/1343145/tsql-pivot-without-aggregate-function http://stackoverflow.com/questions/ 14618316/how-to-create-a-Pivot-Abfrage-in-sql-Server-ohne-aggregate-Funktion – JonWay

+0

@tom warum es dupliziert wurde, was auch immer die Frage nicht einmal eine Meile mit ihm übereinstimmt – mohan111

Antwort

2

Versuchen Sie folgendes:

SELECT id, 
     STUFF((SELECT ',' + name 
       FROM @Table1 t2 
       WHERE t2.id = t1.id AND val = 'match' 
       FOR XML PATH('')), 1, 1, '') AS Match, 
     STUFF((SELECT ',' + name 
       FROM @Table1 t2 
       WHERE t2.id = t1.id AND val = 'nomatch' 
       FOR XML PATH('')), 1, 1, '') AS Nomatch 

FROM @Table1 t1 
GROUP By id 
+0

@betsos Ich weiß das können Sie ... bitte sagen Sie mir in Pivot Ich will es nicht so statisch wie Streichholz oder keine Übereinstimmung – mohan111

+3

Pivot ist statisch selbst. Pivot oder nicht, um eine variable Anzahl von Spalten zu haben, benötigen Sie sowieso dynamische Sql. – Serg

Verwandte Themen