Results 1 to 5 of 5
  1. #1
    SltPhx is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    41

    Count duplicates in a table and display in a query

    Hello All,
    Kindly help me to count the duplicates (based on a field) in a table and display the count along with certain calculated fields using a query.
    I have a table with UserID, Number(Unique) and Internal/External.
    For eg:

    A1 2 Internal
    B1 3 Internal
    A1 4 Internal
    C1 5 External
    A1 6 External
    C1 7 External

    I want the query to display:

    UserID Count Internal(s) External(s)

    A1 3 2 1
    B1 1 1 0


    C1 2 0 2

    Is this possible? Please let me know..
    Thanks much for all your time

    Warmest Regards,
    SLTPHX

  2. #2
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    You're going to need 3 queries to calculate the different counts then a 4th to bring them together.
    First:
    Code:
    SELECT USERID, Count(UserID) AS CountOfUSERID
    FROM tableName
    Second:
    Code:
    SELECT USERID, Count(UserID) AS CountOfInternal
    FROM tableName
    WHERE [Internal/External] = "Internal"
    Third:
    Code:
    SELECT USERID, Count(UserID) AS CountOfExternal
     FROM tableName
    WHERE [Internal/External] = "External"
    Now, after saving each of those queries, you bring them into a 4th one, join on the USERID, then select each item. The result of this query should be what you want

  3. #3
    SltPhx is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    41

    Thumbs up

    Thank you soooooooo much

    I tried what you suggested and it worked awesome

    Although I had to add something to the queries..
    I ran the query you explained and I got this error "You tried to execute a query that does not include the specified expressions as part of an aggregate function" - so I had to use a Group By clause to the queries.. and IT WORKED!


    SLTPHX

  4. #4
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Yea, I totally forgot about the Group by. Anytime you have Sum(), Count(), Max(), Min() etc, you have to do a group by on the "label" field. The Group By is one of the things that totally slip my mind when I'm typing out SQL. My apologies for missing it. Glad you got it working.

  5. #5
    SltPhx is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    41
    Thank you for explaining..

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Count unique records - no duplicates
    By Kevo in forum Queries
    Replies: 4
    Last Post: 08-15-2011, 01:19 AM
  2. Replies: 6
    Last Post: 07-25-2011, 01:54 PM
  3. Display record count
    By jgelpi16 in forum Reports
    Replies: 5
    Last Post: 11-30-2010, 09:02 PM
  4. Don't count duplicates
    By shenberry in forum Queries
    Replies: 1
    Last Post: 10-27-2010, 09:28 PM
  5. Replies: 1
    Last Post: 08-18-2010, 02:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums