Results 1 to 9 of 9
  1. #1
    filla_dilla is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    3

    Only include unique occurances

    I have a table that tracks the lifecycle of documents. Because one document can have multiple things done to it, the "key" I'm using is repeated each time an action is taken on the document. What I'm trying to accomplish is to get a count of the different document types. The document should only be counted once if the "key" is a repeat. For example, my table looks like:

    key date document
    1 7/18/11 word document
    1 7/18/11 word document
    1 7/18/11 word document
    2 7/18/11 word document
    2 7/18/11 word document
    3 7/18/11 Excel
    3 7/18/11 Excel

    In this example, the results I'm trying to obtain are:

    Count document type
    2 word document
    1 Excel

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Hi

    You may like to try this as the SQL for your query:
    Code:
     
    SELECT Count(Temp.document) AS CountOfdocument, Temp.document
    FROM (SELECT YourTableName.document, YourTableName.key FROM YourTableName GROUP BY YourTableName.document, YourTableName.key) as Temp
    GROUP BY Temp.document;
    You will need to enter your own table name where I have YourTableName
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    An aggregate query, with just grouping - will result with:

    1 7/18/11 word document
    2 7/18/11 word document
    3 7/18/11 Excel

    call this query Agg1


    then a second aggregate query, using Agg1 for its source - set up to count the document type will result in:

    2 word document
    1 Excel

    Aggregate queries are found in the icon greek E epsilon in the toolbar/ribbon of query design - refer to your Access text book for further instruction detail.

    hope this helps.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Also, it would be better to aviod using "date" as the name of a field because "Date" is a reserved word in access.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Hi NTC

    I know that the solutions we both offered work, but in your opinion, does either have any merits over the other?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    filla_dilla is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    3

    Smile

    Quote Originally Posted by Bob Fitz View Post
    Hi

    You may like to try this as the SQL for your query:
    Code:
     
    SELECT Count(Temp.document) AS CountOfdocument, Temp.document
    FROM (SELECT YourTableName.document, YourTableName.key FROM YourTableName GROUP BY YourTableName.document, YourTableName.key) as Temp
    GROUP BY Temp.document;
    You will need to enter your own table name where I have YourTableName
    This works great! Thank you, Bob. I didn't ask about it, because I thought I could add it without any problem, but I need to prompt the user for a date range. The actual column name is DATERECIEVED, but I wanted to keep the example simple. Following the original example, this is what I'm adding, but it's not working how it needs to:

    where date between [Start date:] and [End date:]

  7. #7
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Hi

    I think the criteria needs to be in the subquery so
    maybe this is what you need:
    Code:
     
    SELECT Count(Temp.document) AS CountOfdocument, Temp.document
    FROM (SELECT YourTableName.document, YourTableName.key FROM YourTableName WHERE (((YourTableName.date) Between [Start Date:] And [End Date:])) GROUP BY YourTableName.document, YourTableName.key) AS Temp
    GROUP BY Temp.document;
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  8. #8
    filla_dilla is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    3

    Smile

    Quote Originally Posted by Bob Fitz View Post
    Hi

    I think the criteria needs to be in the subquery so
    maybe this is what you need:
    Code:
     
    SELECT Count(Temp.document) AS CountOfdocument, Temp.document
    FROM (SELECT YourTableName.document, YourTableName.key FROM YourTableName WHERE (((YourTableName.date) Between [Start Date:] And [End Date:])) GROUP BY YourTableName.document, YourTableName.key) AS Temp
    GROUP BY Temp.document;
    Thank you so much for this, Bob. I've been trying to figure this out for about a week now!

  9. #9
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    You're welcome. Glad we have been able to help.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Exclude LATE but include empty
    By dhborchardt in forum Queries
    Replies: 2
    Last Post: 04-15-2011, 01:10 PM
  2. Replies: 10
    Last Post: 12-11-2010, 11:01 PM
  3. include zero data where no record
    By Sandy Gomez in forum Access
    Replies: 4
    Last Post: 09-15-2010, 06:23 AM
  4. composite unique key
    By shital in forum Access
    Replies: 1
    Last Post: 07-09-2010, 08:07 AM
  5. how to set a unique ID
    By archie in forum Access
    Replies: 1
    Last Post: 09-08-2009, 04:28 AM

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