Announcement

Collapse
No announcement yet.

Duplikate filtern

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

  • Duplikate filtern

    Hallo zusammen,

    aus folgender Ausgangstabelle möchte ich aus der Spalte "ArtNr" die Duplikate filtern:

    ArtNr | Typ | Bezeichnung | Anzahl
    ---------------------------------------------
    1 | Zertifikat | Aktienzertifikat | 10
    2 | Aktie | Stammaktien | 1
    2 | Aktie | Bezugsr. Aktien| 1
    3 | Aktie | Stammaktien | 12
    4 | Fonds | Immobilien | 2
    4 | Fonds | Aktienfonds | 15

    Das Ergebnis soll so aussehen:

    ArtNr | Typ | Bezeichnung
    ------------------------------------
    1 | Zertifikat | Aktienzertifikat
    2 | Aktie | Stammaktien
    3 | Aktie | Stammaktien
    4 | Fonds | Aktienfonds

    Im Falle eines Duplikats sollen also (wie bei 4) Typ und Bezeichnung in Abhängigkeit der Anzahl (15 > 2) in die neue Tabelle übernommen werden. Wenn die jeweilige Anzahl gleich sein sollte (wie bei 2), soll einer der beiden Einträge übernommen werden (welcher ist egal).

    Ich hoffe, ich habe mein Problem verständlich formuliert!

    Danke schon mal für die Hilfe,

    Christoph

  • #2
    Auch hallo,

    zumindest was die Duplikate betrifft, könnte dieser Blogbeitrag weiterhelfen: http://devtechblog.blogspot.com/2009...-tabellen.html
    MfG
    Cheat-Sheets for Developers / Programming Quotes

    Comment


    • #3
      In Teil 1 holen wir uns mal die eindeutigen Felder ArtNr und Typ (das ist eine sogenannte CTE = CommonTableExpression) und im zweiten Schritt dann die entsprechende Bezeichnung abhängig von der Anzahl dazu. Sieht dann etwa so aus:

      ;with tmp as (select distinct ArtNr, Typ from Tabelle)
      select ArtNr, Typ, (select top 1 bezeichnung from Tabelle t2
      where t2.ArtNr = tmp.ArtNr and t2.Typ = tmp.Typ order by Anzahl desc)
      from tmp


      bye,
      Helmut

      Comment


      • #4
        auch ne lösung:
        Code:
        select
              artnr,
              TYP,
              bezeichnung
        from
        (
              select 
                    a.artnr,
                    a.TYP,
                    b.maxanz,
                    (select top 1 bezeichnung from ausgang where artnr = artnr and typ = typ and anzahl = maxanz) as bezeichnung
              from 
                    dbo.ausgang a,
                    (
                    select
                          artnr,
                          TYP,
                          max(anzahl) as maxanz
                    from
                          ausgang
                    group by
                          artnr,
                          typ
                    ) b
              where
                    a.artnr = b.artnr
                    and
                    a.typ = b.typ
        )x
        group by
              artnr, TYP, bezeichnung

        Comment


        • #5
          ... ist sicher für alle interessant, die noch eine Version kleiner SQL 2005 haben, denn erst seit da gibt es die CTE.

          bye,
          Helmut

          Comment


          • #6
            Super, gleich die erste Lösung hat perfekt funktioniert!

            Danke vielmals!

            Comment


            • #7
              Originally posted by cakl View Post
              auch ne lösung:
              Code:
              select
                    artnr,
                    TYP,
                    bezeichnung
              from
              (
                    select 
                          a.artnr,
                          a.TYP,
                          b.maxanz,
                          (select top 1 bezeichnung from ausgang where artnr = artnr and typ = typ and anzahl = maxanz) as bezeichnung
                    from 
                          dbo.ausgang a,
                          (
                          select
                                artnr,
                                TYP,
                                max(anzahl) as maxanz
                          from
                                ausgang
                          group by
                                artnr,
                                typ
                          ) b
                    where
                          a.artnr = b.artnr
                          and
                          a.typ = b.typ
              )x
              group by
                    artnr, TYP, bezeichnung
              geht kürzer mit HAVING...
              Code:
              SELECT   artnr,
                       TYP
              FROM     <Tabelle> AS a
              JOIN     (SELECT  y.artnr,
                                y.TYP,
                                COUNT (*)
                        FROM  <Tabelle> AS y
                        GROUP BY y.artnr,
                                 y.TYP
                        HAVING COUNT (*) >1
                       )                       AS X  ON    x.ArtNr = A.Artnr
                                                     AND   x.Typ   = A.Typ

              Comment


              • #8
                Originally posted by ebis View Post
                geht kürzer mit HAVING...
                Code:
                SELECT   artnr,
                         TYP
                FROM     <Tabelle> AS a
                JOIN     (SELECT  y.artnr,
                                  y.TYP,
                                  COUNT (*)
                          FROM  <Tabelle> AS y
                          GROUP BY y.artnr,
                                   y.TYP
                          HAVING COUNT (*) >1
                         )                       AS X  ON    x.ArtNr = A.Artnr
                                                       AND   x.Typ   = A.Typ
                oder als CTE mit HAVING
                Code:
                WITH Duplicates AS 
                   (SELECT  y.artnr,
                            y.TYP,
                            COUNT (*)
                    FROM  <Tabelle> AS y
                    GROUP BY y.artnr,
                             y.TYP
                    HAVING COUNT (*) >1
                   ) 
                SELECT   T.ArtNr
                      ,  T.Typ
                      ,  T.Bezeichnung
                FROM     <Tabelle>  AS T
                JOIN     Duplicates AS D ON  D.ArtNr = T.Artnr
                                         AND D.Typ   = T.Typ

                Comment


                • #9
                  Hey ebis, gefällt dir meine Lösung leicht nicht ...

                  bye,
                  Helmut

                  Comment

                  Working...
                  X