Einzelnen Beitrag anzeigen

alzaimar
(Moderator)

Registriert seit: 6. Mai 2005
Ort: Berlin
4.956 Beiträge
 
Delphi 2007 Enterprise
 
#13

Re: "Bedingte" Abfrage über mehrere Tabellen

  Alt 23. Apr 2008, 10:04
Ich rege mich nicht auf, nur ist die Frage komisch, weil die Antwort schon längst fest steht.

Also:
SQL-Code:
Select    m.Id as [ID],
   m.Text as [Text],
   ta.Typ as [Typ],
   ta.Anzahl as [Anzahl],
   t1.[Größe],
   t1.[Gewicht],
   cast (NULL as VarChar (80)) as [Farbel],
   cast (NULL as int) as [Leistung],
   cast (NULL as money) as [Wert]
from   MainTabelle m
   join TabelleAllgemein ta on m.ID = ta.ID
   join TabelleType1 t1 on t1.ID = m.ID
where ta.Typ = 1
union
Select    m.Id as [ID],
   m.Text as [Text],
   ta.Typ as [Typ],
   NULL as [Anzahl],
   NULL as [Größe],
   NULL as [Gewicht],
   t2.Farbe,
   t2.Leistung,
   NULL as [Wert]
from   MainTabelle m
   join TabelleAllgemein ta on m.ID = ta.ID
   join TabelleType2 t2 on t1.ID = m.ID
where ta.Typ = 2
union
Select    m.Id as [ID],
   m.Text as [Text],
   ta.Typ as [Typ],
   NULL as [Anzahl],
   NULL as [Größe],
   NULL as [Gewicht],
   NULL as [Farbe],
   NULL as [Leistung],
   t3.Wert
from   MainTabelle m
   join TabelleAllgemein ta on m.ID = ta.ID
   join TabelleType2 t2 on t1.ID = m.ID
where ta.Typ = 3
Wenn Du das dynamisch haben willst, dann benötigst Du eine Pivot/Crosstabelle.
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat