Announcement

Collapse
No announcement yet.

Ausgabezeilen zählen je Gruppe

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

  • Ausgabezeilen zählen je Gruppe

    Hallo Forum,
    ich habe eine Tabelle mit Auftragspositionen. Nun möchte ich als Ergebnis einer
    Abfrage einen Zähler je Gruppe (Kundennummer). Ich habe dieses (und andere) Forum bereits gequält, habe auch eine Lösung für das komplette Durchnummerieren, jedoch nicht für meine geforderte Abfrage.

    Gegeben ist folgende Tabelle:

    ID DIS KGR KAL
    ------ ---- ----- -----
    1136 K13 P2 5
    1136 K13 R1 6
    1136 K17 P/DA 5
    1136 K24 P2 5
    1136 K24 R1 6
    1141 K13 P4 24
    1141 K36 P4 24
    1142 K27 R3 7
    1142 LF3 G1 15
    1142 LF3 G2 15
    1147 K13 P2 5
    1147 K36 P2 5
    1149 K24 P2 5
    1149 K27 P2 5
    1149 K36 P2 5

    Das Ergebnis soll sein:


    LFD ID DIS KGR KAL
    ----- ------ ---- ----- -----
    1 1136 K13 P2 5
    2 1136 K13 R1 6
    3 1136 K17 P/DA 5
    4 1136 K24 P2 5
    5 1136 K24 R1 6
    1 1141 K13 P4 24
    2 1141 K36 P4 24
    1 1142 K27 R3 7
    2 1142 LF3 G1 15
    3 1142 LF3 G2 15
    1 1147 K13 P2 5
    2 1147 K36 P2 5
    1 1149 K24 P2 5
    2 1149 K27 P2 5
    3 1149 K36 P2 5

    Grupiert werden soll nach 'ID' und nach jedem
    Gruppenwechsel soll wieder bei 1 angefangen werden
    zu zählen.
    Hat irgend jemand eine Idee?
    Ach ja: SQL-Server 2000

    Danke

  • #2
    Hallo Pretorian,

    im MSSQL 2005 geht es von Haus aus, beim MSSQL 2000 muss man noch "selber zählen":

    [highlight=SQL]CREATE TABLE #Dummy
    (ID int,
    DIS varchar(3),
    KGR varchar(4),
    KAL smallint)

    SET NOCOUNT ON
    INSERT INTO #Dummy VALUES (1136, 'K13', 'P2', 5)
    INSERT INTO #Dummy VALUES (1136, 'K13', 'R1', 6)
    INSERT INTO #Dummy VALUES (1136, 'K17', 'P/DA', 5)
    INSERT INTO #Dummy VALUES (1136, 'K24', 'P2', 5)
    INSERT INTO #Dummy VALUES (1136, 'K24', 'R1', 6)
    INSERT INTO #Dummy VALUES (1141, 'K13', 'P4', 24)
    INSERT INTO #Dummy VALUES (1141, 'K36', 'P4', 24)
    INSERT INTO #Dummy VALUES (1142, 'K27', 'R3', 7)
    INSERT INTO #Dummy VALUES (1142, 'LF3', 'G1', 15)
    INSERT INTO #Dummy VALUES (1142, 'LF3', 'G2', 15)
    INSERT INTO #Dummy VALUES (1147, 'K13', 'P2', 5)
    INSERT INTO #Dummy VALUES (1147, 'K36', 'P2', 5)
    INSERT INTO #Dummy VALUES (1149, 'K24', 'P2', 5)
    INSERT INTO #Dummy VALUES (1149, 'K27', 'P2', 5)
    INSERT INTO #Dummy VALUES (1149, 'K36', 'P2', 5)

    -- Aufwendig von Hand im MS SQL 2000
    SELECT *, (SELECT ISNULL(COUNT(*), 0) + 1
    FROM #Dummy AS SUB
    WHERE SUB.ID = MAIN.ID
    AND (SUB.DIS < MAIN.DIS
    OR (SUB.DIS = MAIN.DIS AND SUB.KGR < MAIN.KGR)
    OR (SUB.DIS = MAIN.DIS AND SUB.KGR = MAIN.KGR AND SUB.KAL < MAIN.KAL)
    )
    ) AS LfdNr
    FROM #Dummy AS MAIN
    ORDER BY ID, DIS, KGR, KAL

    -- IM MS SQL 2005 geht es von Haus aus:
    SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID), *
    FROM #DUMMY

    DROP TABLE #Dummy
    [/highlight]
    Olaf Helper

    <Blog> <Xing>
    * cogito ergo sum * errare humanum est * quote erat demonstrandum *
    Wenn ich denke, ist das ein Fehler und das beweise ich täglich

    Comment


    • #3
      Hallo Olaf,
      vielen Dank für Deine Antwort.
      Einfach Perfekt. Vielen Dank für die Unterstützung, klappt einwandfrei!
      Torsten

      Comment

      Working...
      X