Results 1 to 14 of 14
  1. #1
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114

    Counting Specific Duplications in a Single Table

    I have a table that contains names of employees and the names of awards they received. It's all in one table, which I know isn't ideal.

    tblEmployeeAwards

    EmployeeName, AwardName
    John,Nobel
    John, Pulitzer
    Michael, Fulbright
    Jane, Nobel
    Jane, Fulbright

    I would like to know the following:

    1. How many employees received more than one award (answer: 2).
    2. How many employees who received the Nobel prize also received any second prize (Answer: 2).
    3. How many employees who received the Nobel prize also received the Fulbright (answer: 1).



    I am not sure if this is a SQL or VBA question or both.

    Any suggestions?

    Thank you.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Build aggregate (GROUP BY) Totals queries. Access Help has more guidance on using the query builder. Try:

    1. SELECT employeeID, Count(AwardName) As CountAwards FROM tblEmployeeAwards GROUP BY employeeID HAVING Count(AwardName)>0;

    2. SELECT employeeID, Count(IIf(AwardName="Nobel",1,0)) AS CountNobel, Count(IIf(AwardName<>"Nobel",1,0)) AS CountOther GROUP BY employeeID HAVING Count(IIf(AwardName="Nobel",1,0))>0 AND Count(IIf(AwardName<>"Nobel",1,0))>0;

    3. SELECT employeeID, Count(IIf(AwardName="Nobel",1,0)) AS CountNobel, Count(IIf(AwardName="Fulbright",1,0)) AS CountFulbright GROUP BY employeeID HAVING Count(IIf(AwardName="Nobel",1,0))>0 AND Count(IIf(AwardName="Fulbright",1,0))>0;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Quote Originally Posted by June7 View Post
    Build aggregate (GROUP BY) Totals queries. Access Help has more guidance on using the query builder. Try:

    1. SELECT employeeID, Count(AwardName) As CountAwards FROM tblEmployeeAwards GROUP BY employeeID HAVING Count(AwardName)>0;

    2. SELECT employeeID, Count(IIf(AwardName="Nobel",1,0)) AS CountNobel, Count(IIf(AwardName<>"Nobel",1,0)) AS CountOther GROUP BY employeeID HAVING Count(IIf(AwardName="Nobel",1,0))>0 AND Count(IIf(AwardName<>"Nobel",1,0))>0;

    3. SELECT employeeID, Count(IIf(AwardName="Nobel",1,0)) AS CountNobel, Count(IIf(AwardName="Fulbright",1,0)) AS CountFulbright GROUP BY employeeID HAVING Count(IIf(AwardName="Nobel",1,0))>0 AND Count(IIf(AwardName="Fulbright",1,0))>0;
    Thank you. SQL #1, #2, and #3 all return the same results:

    Jane,2
    John,2
    Michael,1

    I need the answer for #1 to be: ReceivedMoreThanAward,2

    I need the answer for #2 to be: ReceivedNobelAndAnotherAward,2

    I need the answer for #3 to be: ReceivedNobelAndFulbright,1

    Note that my table does not have an EmployeeID field, so I replaced all those references with EmployeeName. Maybe that's an issue.

    Also, in the #2 and #3 statements, I need to add "from tblEmployeeAwards" before "GROUP BY." Without that, I got an error.

    Thanks again.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Sorry, did forget the FROM clause. Names do make very poor unique identifiers.

    Do you not want grouping by employee? Just want totals for entire dataset, no employee detail? You want only one record in the output?

    Try this for query 1:

    SELECT Count(Q1.EmployeeID) AS RecievedMoreThanOneAward FROM (SELECT EmployeeID, Count(AwardName) As CountAwards FROM tblEmployeeAwards GROUP BY EmployeeID) AS Q1 WHERE CountAwards>0;

    Do similar construct for the other 2 queries.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    I don't need grouping by employee here, just totals for the entire dataset.

    If I run this new statement, I get 4, but the correct count is 2.

    My table doesn't have an EmployeeID field, so I changed that to EmployeeName.

    What does Q1 refer to?

  6. #6
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    I suppose that ideally I would have both the detail and the summary count I mentioned above. Perhaps that would be a second query that counts the first query.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Q1 is alias name for the nested subquery.

    I don't know your data so can't say why the result is doubled. It shouldn't.

    If you want details as well as summary data, consider a report and use its Grouping & Sorting features with aggregate calcs in footers.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Thanks for the explanation. I ran the query you suggested:

    SELECT Count(Q1.EmployeeID) AS RecievedMoreThanOneAward FROM (SELECT EmployeeID, Count(AwardName) As CountAwards FROM tblEmployeeAwards GROUP BY EmployeeID) AS Q1 WHERE CountAwards>0;

    My entire data is as follows:

    EmployeeName, AwardName
    John,Nobel
    John, Pulitzer
    Michael, Fulbright
    Jane, Nobel
    Jane, Fulbright

    The answer should be 2, but I am getting 3.

  9. #9
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Check if below give some guidelines :


    The MainQueries


    qryEmpCountwithAwardsGreaterThan1
    Code:
    SELECT 
        Count(qryAwardPerEmployeeCountGreaterThan1.EmployeeName) AS CountOfEmployeeName
    FROM 
        qryAwardPerEmployeeCountGreaterThan1;

    -------------------
    qryEmpCountWithNobelAndFulBright
    Code:
    SELECT 
        Count(qryEmpWithNobelAndFulBright.EmployeeName) AS CountOfEmployeeName
    FROM 
        qryEmpWithNobelAndFulBright;

    -------------------
    qryEmpCountWithNobelAndOtherAward
    Code:
    SELECT 
        Count(qryNobelAndOrOtherAwards.qryEmpNobelAward.EmployeeName) AS CountOfqryEmpNobelAward_EmployeeName
    FROM 
        qryNobelAndOrOtherAwards;



    -------------------




    The subQueries




    qryAwardPerEmployeeCountGreaterThan1
    Code:
    SELECT 
        tblEmployeeAwards.EmployeeName, Count(tblEmployeeAwards.AwardName) AS CountOfAwardName
    FROM 
        tblEmployeeAwards
    GROUP BY 
        tblEmployeeAwards.EmployeeName
    HAVING 
        (((Count(tblEmployeeAwards.AwardName))>1));

    -------------------
    qryEmpWithNobelAndFulBright
    Code:
    SELECT 
        qryAwardNobelOrFulbright.EmployeeName, Count(qryAwardNobelOrFulbright.AwardName) AS CountOfAwardName
    FROM 
        qryAwardNobelOrFulbright
    GROUP BY 
        qryAwardNobelOrFulbright.EmployeeName
    HAVING 
        (((Count(qryAwardNobelOrFulbright.AwardName))>1));

    -------------------
    qryAwardNobelOrFulbright
    Code:
    SELECT 
        tblEmployeeAwards.EmployeeName, tblEmployeeAwards.AwardName
    FROM 
        tblEmployeeAwards
    WHERE 
        (((tblEmployeeAwards.AwardName)="Nobel" Or (tblEmployeeAwards.AwardName)="Fulbright"));
    -------------------


    qryNobelAndOrOtherAwards
    Code:
    SELECT 
        qryEmpNobelAward.EmployeeName, qryEmpNobelAward.AwardName, tblEmployeeAwards.EmployeeName, Count(tblEmployeeAwards.AwardName) AS CountOfAwardName
    FROM 
        qryEmpNobelAward 
        INNER JOIN 
        tblEmployeeAwards 
        ON 
        qryEmpNobelAward.EmployeeName = tblEmployeeAwards.EmployeeName
    GROUP BY 
        qryEmpNobelAward.EmployeeName, qryEmpNobelAward.AwardName, tblEmployeeAwards.EmployeeName
    HAVING 
        (((Count(tblEmployeeAwards.AwardName))>1));

    -------------------
    qryEmpNobelAward
    Code:
    SELECT 
        tblEmployeeAwards.EmployeeName, tblEmployeeAwards.AwardName
    FROM 
        tblEmployeeAwards
    WHERE 
        (((tblEmployeeAwards.AwardName)="Nobel"));
    -------------------

    Thanks

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You mean you want to know which awards had multiple employees, not which employees had multiple awards!!

    SELECT Count(Q1.Award) AS MultipleEmployeesReceived FROM (SELECT Award, Count(EmployeeName) As CountEmps FROM tblEmployeeAwards GROUP BY Award) AS Q1 WHERE CountEmps>0;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Thank you, recyan. That works perfectly. I really appreciate your help.

    One follow up question:

    Assuming I want to add the year an award was received to my main table.

    How would I modify the sub queries to include the year in the results next to each employee's name?

    Thanks again. I really appreciate your time writing those queries.

  12. #12
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    None of the Main Queries have Names in the Result. They only have a count as the result.
    You can definitely add a Year to the Main table & include it in the subqueries. But how you will use it will depend on the final output that you are looking for.
    On the side ( important ), Would ideally have a tblEmployees, tblAwards & tblEmployeeAwards in my design, if that is not the scenario at your end. Worth doing it right now.

    Thanks

  13. #13
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Thanks again. That's really helpful. I realize that the normalized model is the way to go and that's the goal.

  14. #14
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Glad to be of help. All the best with your project.

    Thanks

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

Similar Threads

  1. Replies: 1
    Last Post: 02-19-2014, 05:49 PM
  2. Replies: 7
    Last Post: 06-12-2013, 06:36 AM
  3. Counting values after a specific value
    By cardgage in forum Queries
    Replies: 5
    Last Post: 03-12-2013, 07:41 AM
  4. Specific record counting in Reports
    By Duncan in forum Reports
    Replies: 2
    Last Post: 11-08-2012, 02:54 PM
  5. Counting fields in a single record
    By Racingrl in forum Queries
    Replies: 2
    Last Post: 06-11-2012, 10:52 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