Results 1 to 7 of 7

Thread: Finding maximum number of duplicate records in Access Table?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    89

    Resolved Finding maximum number of duplicate records in Access Table?

    Dear members,
    Here is the sample of my Access Table

    ID SecondaryID
    1 101
    1 101
    1 102
    1 103
    1 103
    1 103
    2 104
    2 105
    2 105
    3 106
    3 107
    3 108
    3 108
    3 108
    3 108


    I want to do is get unique ID having greatest number of duplicate SecondaryID…So that my table becomes in this form


    ID SecondaryID
    1 103
    2 105
    3 108

    Please suggest a query for me....thanx
    Last edited by imstillalive; Feb 15th, 2011 at 10:58 AM. Reason: Resolved

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Finding maximum number of duplicate records in Access Table?

    OK, here's what I came up with... first the whole code
    Code:
    DECLARE @MyTable TABLE (ID INT NOT NULL, SecondaryID INT NOT NULL)
    INSERT INTO @MyTable (ID, SecondaryID) Values(1,101)
    INSERT INTO @MyTable (ID, SecondaryID) Values(1,101)
    INSERT INTO @MyTable (ID, SecondaryID) Values(1,102)
    INSERT INTO @MyTable (ID, SecondaryID) Values(1,103)
    INSERT INTO @MyTable (ID, SecondaryID) Values(1,103)
    INSERT INTO @MyTable (ID, SecondaryID) Values(1,103)
    
    INSERT INTO @MyTable (ID, SecondaryID) Values(2,103)
    INSERT INTO @MyTable (ID, SecondaryID) Values(2,105)
    INSERT INTO @MyTable (ID, SecondaryID) Values(2,105)
    
    INSERT INTO @MyTable (ID, SecondaryID) Values(3,106)
    INSERT INTO @MyTable (ID, SecondaryID) Values(3,107)
    INSERT INTO @MyTable (ID, SecondaryID) Values(3,108)
    INSERT INTO @MyTable (ID, SecondaryID) Values(3,108)
    INSERT INTO @MyTable (ID, SecondaryID) Values(3,108)
    INSERT INTO @MyTable (ID, SecondaryID) Values(3,108)
    
    
    SELECT C.ID, C.SecondaryID
    FROM (SELECT ID, SecondaryID, COUNT(*) as RCount
    			FROM @MyTable 
    			GROUP BY ID, SecondaryID) C
    INNER JOIN (SELECT ID, MAX(RCount) AS RCount
    			FROM (SELECT ID, SecondaryID, COUNT(*) as RCount
    						FROM @MyTable 
    						GROUP BY ID, SecondaryID) C2
    			GROUP BY ID) T
    	ON T.ID = C.ID
    		AND T.RCount = C.RCount
    ORDER BY C.ID
    IT was done with SQL Server, but the query itself is simple SQL, so it should still work for you...

    this is the part that you're going to want:
    Code:
    SELECT C.ID, C.SecondaryID
    FROM (SELECT ID, SecondaryID, COUNT(*) as RCount
    			FROM @MyTable 
    			GROUP BY ID, SecondaryID) C
    INNER JOIN (SELECT ID, MAX(RCount) AS RCount
    			FROM (SELECT ID, SecondaryID, COUNT(*) as RCount
    						FROM @MyTable 
    						GROUP BY ID, SecondaryID) C2
    			GROUP BY ID) T
    	ON T.ID = C.ID
    		AND T.RCount = C.RCount
    ORDER BY C.ID
    You'll need to change the table name... but that should work for you.

    The only time you may run into a problem is when you have multiple SecondaryIDs with the same count...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Finding maximum number of duplicate records in Access Table?

    Try this:

    Code:
    SELECT ID, MAX(SecondaryID), MAX(IDCount) FROM (SELECT ID, SecondaryID, Count(*) as IDCount from num group by ID, SecondaryID) GROUP BY ID

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Finding maximum number of duplicate records in Access Table?

    MarMan - I thought of that too... except that if there were a line for ID = 1, Secondary ID = 104 .... the max(SEcondaryID) will return 104, but the count would be from 103... which is incorrect.

    And just to make sure I wasn't crazy... that's what I did... added an additional ID =1 line with 104 as the Secondary ID....
    Here are the results:
    my query
    Code:
    /*
    ID                  SecondaryID         
    ----------------------------------------
    */
    1                   103                 
    2                   105                 
    3                   108
    Yours:
    Code:
    /*
    ID                  Column1             Column2             
    ------------------------------------------------------------
    */
    1                   104                 3                   
    2                   105                 2                   
    3                   108                 4                   
    
    
    /*(3 row(s) affected)*/
    Complete Table Listing:
    Code:
    /*
    ID                  SecondaryID         
    ----------------------------------------
    */
    1                   101                 
    1                   101                 
    1                   102                 
    1                   103                 
    1                   103                 
    1                   103                 
    1                   104                 
    2                   104                 
    2                   105                 
    2                   105                 
    3                   106                 
    3                   107                 
    3                   108                 
    3                   108                 
    3                   108                 
    3                   108                 
    
    
    /*(16 row(s) affected)*/
    Other than that, given the data we were supplied, your query works fine ... I usually get hung up in those kinds of details because it's my job to think about things like that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Finding maximum number of duplicate records in Access Table?

    Thanks for the info tech, you are right, I just tested it with the supplied data.

    This forum will help my improve my SQL!
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    89

    Smile Re: Finding maximum number of duplicate records in Access Table?

    Thanks techgnome and Marman for the reply...i am sorry if i have been not so clear in asking my question

    Tech, i am going through your code and i will reply soon...


    Thanx....

    Code:
    SELECT C.ID, C.SecondaryID
    FROM (SELECT ID, SecondaryID, COUNT(*) as RCount
    			FROM @MyTable 
    			GROUP BY ID, SecondaryID) C
    INNER JOIN (SELECT ID, MAX(RCount) AS RCount
    			FROM (SELECT ID, SecondaryID, COUNT(*) as RCount
    						FROM @MyTable 
    						GROUP BY ID, SecondaryID) C2
    			GROUP BY ID) T
    	ON T.ID = C.ID
    		AND T.RCount = C.RCount
    ORDER BY C.ID

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    89

    Smile Re: Finding maximum number of duplicate records in Access Table?

    Thanx again Techgnome,

    It solved my problem...:

Posting Permissions

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



Click Here to Expand Forum to Full Width