2017-11-15 1 views
1

Dies ist meine SQL-AnweisungVerschwenkung der Daten

select id , name, type, value from table1 a 
INNER JOIN table2 b on a.id = b.id 
where b.type in ('display','contact','ship') 

, die unterhalb Ergebnis führt

ID name  type   value 
5 test  display  display1 
5 test  contact  contact1 
5 test  ship   ship1 
6 test2 display  display2 
6 test2 contact  contact2 
6 test2 ship   ship2 

ich muss führen in Art geschwenkt Format wie dieses

id name display contact ship 
5 test display1 contact1 ship1 
6 test2 display2 contact2 ship2 

Ich habe versucht, Diese Lösung: https://stackoverflow.com/a/6849706/2645738, aber es gibt mir das gleiche Ergebnis (3 Zeilen für jede Daten). Es ist, als ob ich nach ID und Name gruppieren müsste, aber nicht weiß, wie man Anzeige, Kontakt, Schiff als Spalten macht.

Würdest du mir bitte für das gleiche helfen.

Antwort

2

Diese Abfrage sollten Sie die gewünschten Ergebnisse:

select a.id , a.name, 
     max(case when b.type = 'display' then value end) as display, 
     max(case when b.type = 'contact' then value end) as contact, 
     max(case when b.type = 'ship' then value end) as ship 
from table1 a 
INNER JOIN table2 b on a.id = b.id 
where b.type in ('display','contact','ship') 
group by a.id, a.name 
+0

Vielen Dank für Ihre Zeit. Das hat funktioniert. Kann man das so ändern, dass Anzeige, Kontakt und Schiff in einer Variablen als komma-separierte Werte stehen? z.B. DECLARE @Types varchar (max) = 'Anzeige, Kontakt, Schiff'. Ich werde diesen Code in der gespeicherten Prozedur schreiben und diese Werte können in einer einzelnen Variable kommen. – user2645738

4

Es ist notwendig, zu verwenden PIVOT Sie auch tun könnten, dass mit einfachen case Ausdruck

SELECT ID, 
     Name, 
     MAX(CASE([type]) WHEN 'display' THEN value END) [display], 
     MAX(CASE([type]) WHEN 'contact' THEN value END) [contact], 
     MAX(CASE([type]) WHEN 'ship' THEN value END) [ship] 
FROM <table> GROUP BY ID, Name 

Ergebnis:

ID Name display  contact  ship 
5 test display1 contact1 ship1 
6 test2 display2 contact2 ship2 
2

Dieses arbeitete für mich

WITH T 
AS 
(
    SELECT 
     id , 
     name, 
     type, 
     value 
     FROM table1 a 
     INNER JOIN table2 b 
      ON a.id = b.id 
     WHERE b.type in ('display','contact','ship') 
) 
SELECT 
    * 
    FROM T 
    PIVOT 
    (
    MAX([Value]) 
    FOR 
    [Type] IN 
    (
     [display],[Contact],[Ship] 
    ) 
)PVT 

Überprüfen Sie die SQLFiddle

+0

Kein Problem ..... –

2

Wenn Sie PIVOT:

DECLARE @DataSource TABLE 
( 
    [id] TINYINT 
    ,[name] VARCHAR(12) 
    ,[type] VARCHAR(12) 
    ,[value] VARCHAR(12) 
); 

INSERT INTO @DataSource ([id], [name], [type], [value]) 
VALUES (5, 'test', 'display', 'display1') 
     ,(5, 'test', 'contact', 'contact1') 
     ,(5, 'test', 'ship', 'ship1') 
     ,(6, 'test2', 'display', 'display2') 
     ,(6, 'test2', 'contact', 'contact2') 
     ,(6, 'test2', 'ship', 'ship2'); 

SELECT * 
FROM @DataSource 
PIVOT 
(
    MAX([value]) FOR [type] IN ([display], [contact], [ship]) 
) PVT; 

enter image description here

0
select id,name,[display],[contact],[ship] 

from #b 

pivot(max(value) for type in([display],[contact],[ship])) As d 
+2

Eine Code-Only-Antwort liefert dem OP oder denjenigen, die diese Antwort finden, nicht viel. Bitte aktualisieren Sie Ihre Antwort und erläutern Sie, warum Sie der Ansicht sind, dass dies die Frage des OP beantwortet. –

+0

siehe bitte [antworten] – JimHawkins

Verwandte Themen