Während das vorgeschlagene Duplikat einen Teil des Weges dorthin bekommen wird, müssen Sie eigentlich pivotieren und dann pivotieren, so.
(Oh und bitte nicht Bilder DDL geschätzt und spart uns die Eingabe und/oder zu erraten..)
CREATE TABLE #Test(Action char, [Key] INT, Old varchar(5), OldValue varchar(5), New varchar(5), NewValue varchar(5));
INSERT INTO #Test VALUES
('U', 123, 'Col1','Dog','Col1','Dog'),
('U', 123, 'Col2','Cat','Col2','Mouse'),
('U', 123, 'Col3','Honey','Col3','Bee'),
('I', 123, NULL,NULL,'Col45','Sun');
SELECT PVT.Action
,PVT.[Key]
,PVT.OldCol1
,PVT.OldCol2
,PVT.OldCol3
,PVT.NewCol1
,PVT.NewCol2
,PVT.NewCol3
,PVT.NewCol45 FROM (
SELECT [Action]
,[Key]
,Label
,Value
FROM #Test
CROSS APPLY (VALUES ('Old'+Old, OldValue), ('New'+New, NewValue)) c(label, value)
)src
PIVOT
(
MAX(VALUE) FOR Label IN (OldCol1, NewCol1, OldCol2, NewCol2, OldCol3, NewCol3, NewCol45)
)PVT
ORDER BY PVT.Action Desc
Action Key OldCol1 OldCol2 OldCol3 NewCol1 NewCol2 NewCol3 NewCol45
------ ----------- ------- ------- ------- ------- ------- ------- --------
U 123 Dog Cat Honey Dog Mouse Bee NULL
I 123 NULL NULL NULL NULL NULL NULL Sun
(2 row(s) affected)
können Sie einfach mysql rohen zu Spalte mit case Anweisung –
@Jens - Sorry, dass das versehentlich war Ich benutze SQL Server 2012 –
Mögliche Duplikat von [Effizient Zeilen in Spalten in SQL Server konvertieren] (http: // Stackoverflow. com/questions/15745042/effizient-konvertieren-rows-to-columns-in-sql-server) – Ash