Announcement

Collapse
No announcement yet.

Selektion SQL-Statement

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Selektion SQL-Statement

    Hiho!
    Hab da nen Kleines Problem.
    Ich habe eine Tabelle, in der immer 2 bis n Elemente sind die zueinander gehören. Ich brauche jetzt ein select-statement, das mir Volgendes zurrückgeben soll:
    Code:
    zuordnung  a       b      c
    1         5       8      -
    1         -        -      1
    2         4       6      -
    2         -        -       2
    2         -       -       3
    2         -       -      -4
    3         5      3       -
    die Tabelle sieht ähnlich aus wie das hier:

    Code:
    id zurodnung (abc)  ist
    1    1                 5     a
    2    1                 8     b
    3    1                 1     c
    4    2                 4     a
    5    2                 6     b
    6    2                 2     c
    7    2                 3     c
    8    2                -4     c
    9    3                 5     a
    ...

    Das ganze Problem ist noch etwas komplexer^^, ich habs nen bischen vereinfacht um mein Problem zu verdeutlichen.

    Ich benutze jetzt SQLite, habe vor ner Zeit nen bischen mit Access gearbeitet.
    Daher kenne ich GroupBy, unter Access gibt es dann First und Last um sich das erste und letzte Element einer Gruppierung anzusehn, aber ich will ja auf mehr als das erste und das letzte Element zugreifen, also am liebsten wohl auf 2 bis n Elemente.
    Code:
    SELECT Tabelle1.ID1, Tabelle1.text, First(Tabelle1.text) AS ErsterWertvontext
    FROM Tabelle1
    GROUP BY Tabelle1.ID1, Tabelle1.feld, Tabelle1.text
    HAVING (((Tabelle1.feld)=1));
    So waren meine Ansätze in Access...
    Ich hoffe meine Problem war verständlich und, dass es eine Lösung gibt :-)

  • #2
    [highlight="sql"]
    select
    t1.zuordnung, t1.ist as a,
    t2.ist as b, t3.ist as c
    from
    tabelle t1
    join tabelle t2 on t2.zuordnung = t1.zuordnung and t2.art = 'b'
    join tabelle t3 on t3.zuordnung = t1.zuordnung and t3.art = 'c'
    where
    t1.art='a';
    [/highlight]

    Comment


    • #3
      hmmm danke für die Antwort, aber so ganz ist das nicht was ich suche.
      So krieg ich für jeden eintrag art='c' eine Ganze spalte zuordnung,a,b,c
      und wenn es keinen Eintrag art='c' gibt krieg ich gar nix.
      Ich will also
      A--B--C
      1--2--0
      0--0--1
      0--0--3
      0--0--2
      Sowas, sind eigentlich zwei Ergebnisse...
      Also a und b ist richtig, nur die müssen immer wenn vorhanden angezeigt werden und c in ne extra reihe, wenn das verständlich ist ;-)

      Comment


      • #4
        Also eigentlich sollte genau das geliefert werden, was du in deiner ursprünglichen Frage wolltest.
        Deine erweiterte Frage verstehe ich nicht.

        Comment


        • #5
          zu dem beispiel mal zurrück:

          Das wollte ich :
          zuordnung a b c
          1 5 8 -
          1 - - 1
          2 4 6 -
          2 - - 2
          2 - - 3
          2 - - -4
          3 5 3 -
          und das:
          zuordnung a b c
          1 5 8 1
          2 4 6 2
          2 4 6 3
          2 4 6 -4
          hab ich bekommen.
          jetzt verstanden? ^^

          Comment


          • #6
            [HIGHLIGHT="sql"]
            SELECT
            t1.zuordnung,
            IIF(t1.ist is NULL, '-', t1.ist) AS a,
            IIF(t2.ist is NULL, '-', t2.ist) AS b,
            IIF(t3.ist is NULL, '-', t3.ist) AS c
            FROM
            tabelle t1
            JOIN tabelle t2 ON t2.zuordnung = t1.zuordnung AND t2.art = 'b'
            JOIN tabelle t3 ON t3.zuordnung = t1.zuordnung AND t3.art = 'c'
            WHERE
            t1.art='a';
            [/highlight]

            Comment


            • #7
              oO jetzt gehts los werd ich gleich mal testen! Dank dir erstmal^^

              Comment


              • #8
                IFF gibts leider in SQLite nicht, kann man aber wohl mit der Sign Funktion machen, werd ich die tage mal Testen, aber erstmal ist jetzt Wochenende, wenn ich das ganze mal umgesetzt habe werd ich mich noch mal melden...
                so far joeuser

                Comment


                • #9
                  Alternativen wären
                  CASE....WHEN
                  COALESCE()
                  IF()

                  Comment


                  • #10
                    in diesem SQLite, gibt es nur diese CASE WHEn Anweisung.
                    Aber mit der Syntax check ich das nicht:

                    CASE
                    WHEN t1.ist =NULL THEN "-" AS a
                    END,
                    Syntax Error,
                    http://www.sqlite.org/lang_expr.html
                    Da wird die Syntax beschrieben,.
                    Der Fehler liegt bei dem "AS a", kann man das irgendwie anders formulieren oder ne Variable delarieren?
                    Zuletzt editiert von joeuser; 18.02.2008, 14:35.

                    Comment


                    • #11
                      [highlight="sql"]
                      ...
                      case when t1.ist is NULL thrn '-' else t1.ist end as a
                      ...
                      [/highlight]

                      Comment


                      • #12
                        Jo das funtzt wunderbar!
                        Nur ist mein Problem damit nicht behoben, ich will immer a/b in einer Zeile und C in der nächsten ohne a/b.
                        Jetzt habe ich A/B in einer Zeile, wenn ein C noch da ist, dann hab ich dieses C noch in der Selben reihe, nicht so schlimm^^, aber wenn ich 5 mal C zugeordnet an A/B habe, dann wird auch 5 mal A/B angezeigt, dabei will ich das nur einmal ^^.
                        Ich weiß ich hab komische Probleme ...
                        gz zum 700tsten Beitrag

                        Comment


                        • #13
                          Dann lass mal die 3. Spalte weg und füge nur diese in einem UNION hinzu

                          Comment


                          • #14
                            du meinst für diese Spalte:
                            JOIN tabelle t3 ON t3.zuordnung = t1.zuordnung AND t3.art = 'c'
                            ein
                            UNION SELECT t3.ist FROM Tabelle t3 WHERE t3.art = 'c'
                            ganz am ende?

                            Darf man denn "Tabelle t3" so überhaubt ableiten? Oder muss da nen AS oder = oder so hin?
                            Zuletzt editiert von joeuser; 20.02.2008, 11:35.

                            Comment


                            • #15
                              [highlight="sql"]
                              SELECT
                              t1.zuordnung as zuordnung,
                              CASE WHEN t1.ist IS NULL THEN '-' ELSE t1.ist end as a,
                              CASE WHEN t2.ist IS NULL THEN '-' ELSE t2.ist end as b,
                              FROM
                              tabelle t1
                              JOIN tabelle t2 ON t2.zuordnung = t1.zuordnung AND t2.art = 'b'
                              WHERE
                              t1.art='a'
                              UNION
                              SELECT
                              t3.zuordnung as Zuordnung,
                              '-' as a,
                              '-' as b,
                              CASE WHEN t3.ist IS NULL THEN '-' ELSE t3.ist end as c
                              FROM
                              tabelle t3

                              WHERE
                              t3.art='b
                              ORDER by Zuordnung;
                              [/highlight]

                              Comment

                              Working...
                              X