Ankündigung

Einklappen
Keine Ankündigung bisher.

Minimum filtern

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • #16
    hmmm, hat das einen Syntaxfehler? Ich seh ihn nicht.

    Kommentar


    • #17
      Zitat von stackoverflow Beitrag anzeigen
      hmmm, hat das einen Syntaxfehler? Ich seh ihn nicht.
      Code:
      SELECT id, text_id, MIN(position)
                    FROM texte
                    WHERE text_id IN ($text_IDs_string)
                    GROUP BY text_id
      Alle Spalten des Resultsets müssen entweder aggregiert oder gruppiert werden. Was ist mit id? JEDE andere DB wirft Dir hier einen Fehler, MySQL liefert mehr oder weniger Zufall.
      PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

      Kommentar


      • #18
        Ich will ja eigentlich über id keine Gruppe (Aggregat) bilden und dachte, dieses id wirkt so, dass einfach jeweils die zu den Ergebnissen passenden id ausgegeben werden.
        Wie sieht denn die richtige Syntax aus?

        Kommentar


        • #19
          Zitat von stackoverflow Beitrag anzeigen
          Ich will ja eigentlich über id keine Gruppe (Aggregat) bilden und dachte, dieses id wirkt so, dass einfach jeweils die zu den Ergebnissen passenden id ausgegeben werden.
          Wie sieht denn die richtige Syntax aus?
          Mit Deinen Zahlen und PostgreSQL:

          Code:
          test=# select distinct on(text_id) id, text_id, position from stackoverflow order by text_id, position, id;
           id | text_id | position
          ----+---------+----------
            2 |      10 |        1
            5 |      11 |        6
            6 |      12 |        1
            8 |      13 |        5
          (4 rows)
          Dein Code würde liefern:

          Code:
          test=*# select id, text_id, min(position) from stackoverflow group by text_id order by position asc;
          ERROR:  column "stackoverflow.id" must appear in the GROUP BY clause or be used in an aggregate function at character 8
          STATEMENT:  select id, text_id, min(position) from stackoverflow group by text_id order by position asc;
          ERROR:  column "stackoverflow.id" must appear in the GROUP BY clause or be used in an aggregate function
          LINE 1: select id, text_id, min(position) from stackoverflow group b...
                         ^
          test=*#
          Daher:

          Code:
          test=*# select * from stackoverflow where (text_id, position) in (select text_id, min(position) from stackoverflow group by text_id) order by text_id;
           id | text_id | position
          ----+---------+----------
            2 |      10 |        1
            5 |      11 |        6
            6 |      12 |        1
            8 |      13 |        5
          (4 rows)
          Auch möglich:

          Code:
          test=*# select id, text_id, position from (select *, row_number() over (partition by text_id order by position) from stackoverflow ) foo where row_number = 1;
           id | text_id | position
          ----+---------+----------
            2 |      10 |        1
            5 |      11 |        6
            6 |      12 |        1
            8 |      13 |        5
          (4 rows)
          aber natürlich nur mit nicht-behinderten Datenbanken.
          PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

          Kommentar

          Lädt...
          X