Anordnung von Zeile in Spalte [PIVOT]

lazur2006

Benutzer
Beiträge
7
Hallo Zusammen,

ich habe ein Problem mit der Struktur einer Tabelle.
Als Beispiel:
upload_2016-3-30_12-21-41.png

Nun soll diese Struktur überführt werden zu folgender. (Die farblich unterschiedlich markierten Bereiche spiegeln zusammengehörende Messungen wieder.)

upload_2016-3-30_12-25-56.png

Ich komme mit meinem Ansatz nicht sehr weit.

Danke schonmal für eure Hilfe.
 
Werbung:
Code:
SELECT   t0.Datum,
     t0.DMC,
     t0.Produkt,
     t1.[Description] AS Gain,
     t2.[Description] AS [Min],
     t3.[Description] AS [Max],
     t4.[Description] AS [Offset],
     t5.[Description] AS Rough,
     t6.[Description] AS Rough_min
FROM   (

SELECT   DISTINCT
     t0.Datum,
     t0.DMC,
     t0.Produkt
FROM   tabelle

     ) t
INNER JOIN tabelle t1
ON     t0.Datum = t1.Datum
AND     t0.DMC = t1.DMC
AND     t0.Produkt = t1.Produkt
AND     t1.[Description] = 'Gain'
INNER JOIN tabelle t2
ON     t0.Datum = t2.Datum
AND     t0.DMC = t2.DMC
AND     t0.Produkt = t2.Produkt
AND     t2.[Description] = 'Min'
INNER JOIN tabelle t3
ON     t0.Datum = t3.Datum
AND     t0.DMC = t3.DMC
AND     t0.Produkt = t3.Produkt
AND     t3.[Description] = 'Max'
INNER JOIN tabelle t4
ON     t0.Datum = t4.Datum
AND     t0.DMC = t4.DMC
AND     t0.Produkt = t4.Produkt
AND     t4.[Description] = 'Offset'
LEFT JOIN tabelle t5
ON     t0.Datum = t5.Datum
AND     t0.DMC = t5.DMC
AND     t0.Produkt = t5.Produkt
AND     t5.[Description] = 'ROUGH'
LEFT JOIN tabelle t6
ON     t0.Datum = t6.Datum
AND     t0.DMC = t6.DMC
AND     t0.Produkt = t6.Produkt
AND     t6.[Description] = 'ROUGH_min'
 
PS: Bei einem INNER JOIN macht natürlich der Subselect mit dem DISTINCT nicht viel Sinn, das könnte man also gut kürzen:
Code:
SELECT   t0.Datum,
     t0.DMC,
     t0.Produkt,
     t0.[Description] AS Gain,
     t2.[Description] AS [Min],
     t3.[Description] AS [Max],
     t4.[Description] AS [Offset],
     t5.[Description] AS Rough,
     t6.[Description] AS Rough_min
FROM   tabelle t0
INNER JOIN tabelle t2
ON     t0.Datum = t2.Datum
AND     t0.DMC = t2.DMC
AND     t0.Produkt = t2.Produkt
AND     t2.[Description] = 'Min'
INNER JOIN tabelle t3
ON     t0.Datum = t3.Datum
AND     t0.DMC = t3.DMC
AND     t0.Produkt = t3.Produkt
AND     t3.[Description] = 'Max'
INNER JOIN tabelle t4
ON     t0.Datum = t4.Datum
AND     t0.DMC = t4.DMC
AND     t0.Produkt = t4.Produkt
AND     t4.[Description] = 'Offset'
LEFT JOIN tabelle t5
ON     t0.Datum = t5.Datum
AND     t0.DMC = t5.DMC
AND     t0.Produkt = t5.Produkt
AND     t5.[Description] = 'ROUGH'
LEFT JOIN tabelle t6
ON     t0.Datum = t6.Datum
AND     t0.DMC = t6.DMC
AND     t0.Produkt = t6.Produkt
AND     t6.[Description] = 'ROUGH_min'
WHERE   t0.[Description] = 'Gain'
 
Super, danke dir für deine schnelle Hilfe.
Vll. könntest du mir noch bei 2 Sachen helfen.
upload_2016-3-30_13-33-4.jpeg
Die Werte werden dort nicht angezeigt sonder die Descriptions.

Wie müsste ich vorgehen wenn es die Tabelle noch nicht gibt sondern aus einer vorherigen Anfrage erstellt wurde.

Das wäre auch schon alles. Sorry für die Fragen die hier schon jeder wohl tausende male gehört hat. Habe mich die Woche in ein Projekt eingearbeitet und mein erster Ansatz war eine LabView basierte Lösung habe mich damit verzettelt und jetzt eilt es natürlich wieder.

Beste Grüße
 
Nun da gibt es verschiedene Möglichkeiten.

Das naheliegenste wäre die Abfragen zu kombinieren. Da du mehrfach auf die nicht vorhandene Tabelle zugreifst kann das natürlich unübersichtlich werden, ich kenne ja deine Ausgangsabfrage nicht. Eventuell macht es mehr Sinn, die vorhergende Abfrage als Sicht anzulegen und diese auf die Sicht auszuführen.
 
Okay, dass klingt logisch.
Könntest du mir vielleicht noch kurz erklären wie ich die Werte statt die Descriptions einegbunden bekomme.
Siehe ersten zwei Beispiel-Tabellen oben.
 
Achso ja:
Code:
SELECT   t0.Datum,
     t0.DMC,
     t0.Produkt,
     t0.Wert AS Gain,
     t2.Wert AS [Min],
     t3.Wert AS [Max],
     t4.Wert AS [Offset],
     t5.Wert AS Rough,
     t6.Wert AS Rough_min
FROM   tabelle t0
INNER JOIN tabelle t2
ON     t0.Datum = t2.Datum
AND     t0.DMC = t2.DMC
AND     t0.Produkt = t2.Produkt
AND     t2.[Description] = 'Min'
INNER JOIN tabelle t3
ON     t0.Datum = t3.Datum
AND     t0.DMC = t3.DMC
AND     t0.Produkt = t3.Produkt
AND     t3.[Description] = 'Max'
INNER JOIN tabelle t4
ON     t0.Datum = t4.Datum
AND     t0.DMC = t4.DMC
AND     t0.Produkt = t4.Produkt
AND     t4.[Description] = 'Offset'
LEFT JOIN tabelle t5
ON     t0.Datum = t5.Datum
AND     t0.DMC = t5.DMC
AND     t0.Produkt = t5.Produkt
AND     t5.[Description] = 'ROUGH'
LEFT JOIN tabelle t6
ON     t0.Datum = t6.Datum
AND     t0.DMC = t6.DMC
AND     t0.Produkt = t6.Produkt
AND     t6.[Description] = 'ROUGH_min'
WHERE   t0.[Description] = 'Gain'
 
Ich noch einmal,
habe versucht die vorherige Abfrage einzusetzen. Villeicht könnte ich dich ja noch einmal begeistern drüber zu schauen.
Vielen Dank.

Code:
SELECT  t0.Datum,
        t0.DMC,
        t0.Produkt,
        t1.[Wert] AS Gain,
        t2.[Wert] AS [Min],
        t3.[Wert] AS [Max],
        t4.[Wert] AS [Offset],
        t5.[Wert] AS Rough,
        t6.[Wert] AS Rough_min

        FROM (select Measures.dt as Datum, DMC, productidentifier as Produkt,MeasureDefinitions.Description, itemvalue as Wert from Measures join MeasureDefinitions on fk_dataItem = MeasureDefinitions.id
                                                    join Tracking on fk_tracking = Tracking.id
                                                    join TrackingIDs on fk_dmcId = TrackingIDs.id
                                                    join StationDefinitions on Tracking.fk_datagroup = StationDefinitions.id
                                                    join ProductDefinitions on Tracking.fk_product = ProductDefinitions.id
                                                    where  tracking.fk_datagroup = 1
                                                    and measures.fk_dataItem in (1649, 1650, 1651, 1652, 1735, 1736, 1737)
                                                    and measures.dt >= '21.03.2015' and Measures.dt < '22.03.2015') t0

INNER JOIN tabelle t1
ON        t0.Datum = t1.Datum
AND     t0.DMC = t1.DMC
AND     t0.Produkt = t1.Produkt
AND     t1.[Description] = 'DAC_Offset'
INNER JOIN tabelle t2
ON        t0.Datum = t2.Datum
AND     t0.DMC = t2.DMC
AND     t0.Produkt = t2.Produkt
AND     t2.[Description] = 'DAC_Gain'
INNER JOIN tabelle t3
ON        t0.Datum = t3.Datum
AND     t0.DMC = t3.DMC
AND     t0.Produkt = t3.Produkt
AND     t3.[Description] = 'DAC_Gain_max'
INNER JOIN tabelle t4
ON        t0.Datum = t4.Datum
AND     t0.DMC = t4.DMC
AND     t0.Produkt = t4.Produkt
AND     t4.[Description] = 'DAC_Gain_min'
LEFT JOIN tabelle t5
ON        t0.Datum = t5.Datum
AND     t0.DMC = t5.DMC
AND     t0.Produkt = t5.Produkt
AND     t5.[Description] = 'sng_Gain'
LEFT JOIN tabelle t6
ON        t0.Datum = t6.Datum
AND     t0.DMC = t6.DMC
AND     t0.Produkt = t6.Produkt
AND     t6.[Description] = 'sng_Gain_max'
where    t0.[Description] = 'DAC_Offset'

Aus dieser Abfrage:
Code:
select Measures.dt as Datum, DMC, productidentifier as Produkt,MeasureDefinitions.Description, itemvalue as Wert from Measures join MeasureDefinitions on fk_dataItem = MeasureDefinitions.id
                                                    join Tracking on fk_tracking = Tracking.id
                                                    join TrackingIDs on fk_dmcId = TrackingIDs.id
                                                    join StationDefinitions on Tracking.fk_datagroup = StationDefinitions.id
                                                    join ProductDefinitions on Tracking.fk_product = ProductDefinitions.id
                                                    where  tracking.fk_datagroup = 1
                                                    and measures.fk_dataItem in (1649, 1650, 1651, 1652, 1735, 1736, 1737)
                                                    and measures.dt >= '21.03.2015' and Measures.dt < '22.03.2015'
ensteht folgende Tabelle:
upload_2016-3-30_15-34-58.jpeg
 
Also die "plumpe" Variante die Abfragen zu kombinieren wäre das ursprüngliche Statement überall dort einzubauen, wo bisher nur tabelle steht. Du hast das an der ersten Stelle gemacht, müsstest das aber mehrfach machen. - Das das ziemlich unperformant und unüberschaubar wird dürfte klar sein.

Die vernünftige Art wäre sich die Tabellenstruktur anzugucken und zu verstehen, was wann wo steht. Um sich dann in jedem Fall nur die benötigte Information zu holen. Das ist ziemlich komplex, arbeitsintensiv und nur anhand deines Ausgangsselects nicht nachvollziehbar. Abgesehen davon das es mich viel Zeit kosten würde :-/

Mit deinem Ausgangsselect als Sicht wirst du am besten fahren.
 
Werbung:
Zurück
Oben