SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    duplicate records multiple columns

    Hello,

    I would like to find duplicate records based off two columns and then also get the unique ID / index field? Is this possible since I am doing a GROUP BY?

    I want to delete duplicate records and it would be easier if I had the ID.

    Thanks,
    Bart

    Code MySQL:
     
    SELECT DirID, COUNT(*), RecordID, COUNT(*)
    FROM Instance
    GROUP BY DirID,RecordID
    HAVING COUNT(*) > 1

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What is the table key? DirID/RecordID?
    If so, you were almost there. Just eliminate the first COUNT(*) in your SELECT.

  3. #3
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Table Key is InstID, so there are 3 fields total:

    InstID, DirID, RecordID

  4. #4
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    696
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select InstID,
        DirID,
        RecordID
      from instance
     where (DirID,RecordID) in
         (select DirID,recordID
            from Instance
           group by DirID,RecordID
          having count(*) > 1)

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT Instance.InstID
         , Instance.DirID
         , Instance.RecordID
      FROM ( SELECT DirID
                  , RecordID
               FROM Instance
             GROUP 
                 BY DirID
                  , RecordID
             HAVING COUNT(*) > 1 ) AS dupes
    INNER
      JOIN Instance
        ON Instance.DirID    = dupes.DirID   
       AND Instance.RecordID = dupes.RecordID
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you swampboogie and r937. Both seemed to work! This is exactly what I needed. Thank you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •