Results 1 to 3 of 3
  1. #1
    msadiqrajani is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    12

    Need Help With Top 2 By group

    Hello


    I have data set with below formatting and i am trying create query to get top two of each group(group by div and sub div)

    Suppose I have this data:
    DIVISION SUBDIVISION TRADE SumOfTotProblems
    IND INDA P1 20
    IND INDA P3 12
    IND INDB P1 15
    IND INDA P2 25
    IND INDB P3 15
    IND INDB P2 30
    AUS AUSA P1 23
    AUS AUSA P3 37
    AUS AUSA P2 27
















    Then result should be:
    DIVISION SUBDIVISION TRADE SumOfTotProblems
    IND INDA P2 25
    IND INDA P1 20
    IND INDB P2 30
    IND INDB P1 15
    AUS AUSA P3 37
    AUS AUSA P2 27












    I tried this query....but its not working and throwing error:
    Code:
    Error:
    this expression is typed incorrectly or it is too complex to be evaluated
    Code:
    Query:
    
    SELECT Division, Subdivision , Trade, SumOfTotProblems
    FROM Helpertop5 
    WHERE
    SumOfTotProblems IN 
    (SELECT TOP 3 SumOfTotProblems
    FROM Helpertop5 HT WHERE Helpertop5.Division = HT.Division AND Helpertop5.Subdivision = HT.Subdivision
    ORDER BY Helpertop5.SumOfTotProblems DESC)
    ORDER BY Helpertop5.SumOfTotProblems DESC;
    Please help me to figure out how I can do this.
    Thanks

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    I built a table per the example and pasted your SQL into query. It ran without error message but returns all 9 records.

    This returns 7 records because there is a tie.

    SELECT *
    FROM Helpertop5
    WHERE SumOfTotProblems IN
    (SELECT Top 2 SumOfTotProblems AS S_D FROM Helpertop5 AS HT
    WHERE HT.SubDivision = Helpertop5.SubDivision And HT.Division = Helpertop5.Division
    ORDER BY SumOfTotProblems DESC)
    ORDER BY SubDivision, Division;

    Tie breaker would require a unique ID. Review http://allenbrowne.com/subquery-01.html
    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.

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

Similar Threads

  1. Group and Sum
    By leftcoast in forum Queries
    Replies: 5
    Last Post: 08-31-2011, 04:30 PM
  2. query group by over another group by?
    By gap in forum Queries
    Replies: 2
    Last Post: 07-04-2011, 12:59 AM
  3. Sum without Group By
    By Beorn in forum Queries
    Replies: 1
    Last Post: 01-07-2011, 08:54 AM
  4. Replies: 5
    Last Post: 11-29-2010, 08:16 AM
  5. columns for group detail but not group header?
    By Coolpapabell in forum Reports
    Replies: 0
    Last Post: 08-21-2009, 08:53 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