Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Secureuser.cm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2015
    Posts
    8

    Filter top N results by field

    I have a sum query where I have 3 fields. DC which contains for example 10 DC location names. Qty field that is sum based and a cost field that is sum based and sorted in ascending order. When I filter the query through the top values for 25 I get the top 25 items by cost field. But I need top 25 by cost for each of the 10 DC field location names.


    Can't figure out how to do this. Not sql proficient

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    I have a 'report' table for my top X. It has 5 columns, name,amt, rankFld1,RankFld2,RankFld3
    I append the 1st batch, the top 25 of Field1
    then 2nd batch of top 25 of fld2
    then fld3.

    each of the 3 queries sums,and ranks the leaders.
    then sum the report table to show the results.

  3. #3
    Secureuser.cm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2015
    Posts
    8
    Not sure if that will work for me as the table has 8 million records. I'm running 5 back end data bases each with data for the month and year. Is there no other way to just query the it rather then making new tables?

  4. #4
    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,870
    Post the SQL view of the latest query attempt.

    See Top N in this allen Browne article

  5. #5
    Secureuser.cm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2015
    Posts
    8
    Quote Originally Posted by orange View Post
    Post the SQL view of the latest query attempt.

    See Top N in this allen Browne article

    This gives me the Top Items sorted by Cost for the Top 25. but I need the Top 25 sorted by cost for Each DC Field


    SELECT [Q1 Inv Adj CC Query Reports Feb].DC, [Q1 Inv Adj CC Query Reports Feb].Facility, [Q1 Inv Adj CC Query Reports Feb].Item, [Q1 Inv Adj CC Query Reports Feb].Description, Sum([Q1 Inv Adj CC Query Reports Feb].[Inv Adj Qty]) AS [SumOfInv Adj Qty], Sum([Q1 Inv Adj CC Query Reports Feb].[Total Cost]) AS [SumOfTotal Cost]
    FROM [Q1 Inv Adj CC Query Reports Feb]
    GROUP BY [Q1 Inv Adj CC Query Reports Feb].DC, [Q1 Inv Adj CC Query Reports Feb].Facility, [Q1 Inv Adj CC Query Reports Feb].Item, [Q1 Inv Adj CC Query Reports Feb].Description
    HAVING (((Sum([Q1 Inv Adj CC Query Reports Feb].[Total Cost]))<0))
    ORDER BY Sum([Q1 Inv Adj CC Query Reports Feb].[Total Cost]);

  6. #6
    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,870
    How does this give top 25?

    I gave you a link to subqueries and Top N as example. You need to set up logic/grouping to match your needs using the concept/approach from the article.

    Can you post a sample of your data as excel or csv?

  7. #7
    Secureuser.cm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2015
    Posts
    8
    Sorry I attached the wrong SQL

    SELECT TOP 25 [Q1 Inv Adj CC Query Reports Feb].DC, [Q1 Inv Adj CC Query Reports Feb].Facility, [Q1 Inv Adj CC Query Reports Feb].Item, [Q1 Inv Adj CC Query Reports Feb].Description, Sum([Q1 Inv Adj CC Query Reports Feb].[Inv Adj Qty]) AS [SumOfInv Adj Qty], Sum([Q1 Inv Adj CC Query Reports Feb].[Total Cost]) AS [SumOfTotal Cost]
    FROM [Q1 Inv Adj CC Query Reports Feb]
    GROUP BY [Q1 Inv Adj CC Query Reports Feb].DC, [Q1 Inv Adj CC Query Reports Feb].Facility, [Q1 Inv Adj CC Query Reports Feb].Item, [Q1 Inv Adj CC Query Reports Feb].Description
    HAVING (((Sum([Q1 Inv Adj CC Query Reports Feb].[Total Cost]))<0))
    ORDER BY Sum([Q1 Inv Adj CC Query Reports Feb].[Total Cost]);

  8. #8
    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,870

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Did you review the Top N Records Per Group section in Allen's article?
    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.

  10. #10
    Secureuser.cm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2015
    Posts
    8
    Network CC Sum N Query Feb.zip

    Here is a sample of the data. the actual query returns over 100k records. I did read the attached file but I understand that I need to do a Subquery in SQL but I have not learned SQL yet I am Self taught and do everything in Design View.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Also mostly self-taught. I learned SQL by example. I followed Allen's example and adjusted for my tables/fields.

    Will need a unique identifier field - autonumber will serve. Based on the posted Excel, do this test:

    SELECT * FROM Sheet1 WHERE ID IN (SELECT TOP 5 ID FROM Sheet1 AS Dupe WHERE Dupe.DC=Sheet1.DC ORDER BY Dupe.DC, Dupe.[SumOfTotal Cost] DESC) ORDER BY DC, [SumOfTotal Cost] DESC;

    However, all the numbers are negative so maybe you want ASC sort order.
    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.

  12. #12
    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,870
    Are your data values all negative, really?
    I agree with June's sql

    I reformatted a little as
    Code:
    SELECT *
    FROM Network_cc_sum_N_query_Feb
    WHERE ID IN (
            SELECT TOP 5 ID
            FROM Network_cc_sum_N_query_Feb AS Dupe
            WHERE Dupe.DC = Network_cc_sum_N_query_Feb.DC
            ORDER BY Dupe.[SumOfTotal Cost] DESC
                           ,DC
            )
    ORDER BY DC
        ,[SumOfTotal Cost] DESC;
    Last edited by orange; 08-14-2015 at 03:03 PM. Reason: added sql

  13. #13
    Secureuser.cm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2015
    Posts
    8
    I have a query for Neg numbers and a query for Pos numbers

  14. #14
    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,870
    ????Why???

    It is easier for readers to respond with focus when they have a clear understanding of WHAT you are trying to do. This is often most communicative if it is plain English - no jargon, no attempt to quasi-code something. The readers are human volunteers- no special powers, not clairvoyant.

    Why are there 2 sets of values -- positive and negative?
    Would you consider 400 queries if you had 400 employees?

    Did you try the SQL/query that was provided? Results??

  15. #15
    Secureuser.cm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2015
    Posts
    8
    Quote Originally Posted by orange View Post
    Are your data values all negative, really?
    I agree with June's sql

    I reformatted a little as
    Code:
    SELECT *
    FROM Network_cc_sum_N_query_Feb
    WHERE ID IN (
            SELECT TOP 5 ID
            FROM Network_cc_sum_N_query_Feb AS Dupe
            WHERE Dupe.DC = Network_cc_sum_N_query_Feb.DC
            ORDER BY Dupe.[SumOfTotal Cost] DESC
                           ,DC
            )
    ORDER BY DC
        ,[SumOfTotal Cost] DESC;


    getting error saying can not locate query or table,, I found it added ; to the SQL statement when I pasted it. I removed and it is asking me to input a criteria in a pop up box for ID?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to save results from a filter into a table
    By jmmunoz2141 in forum Access
    Replies: 2
    Last Post: 03-04-2014, 09:09 AM
  2. Using a query to filter - too many results
    By pbouk in forum Queries
    Replies: 9
    Last Post: 03-14-2013, 11:26 AM
  3. Replies: 1
    Last Post: 08-16-2012, 01:51 PM
  4. Look Up Tables and Filter Results
    By starkeymd in forum Access
    Replies: 1
    Last Post: 01-12-2012, 04:17 PM
  5. cbo to filter results from a query
    By nianko in forum Forms
    Replies: 5
    Last Post: 08-18-2010, 09:43 AM

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