Page 3 of 3 FirstFirst 123
Results 31 to 38 of 38
  1. #31
    Mubz12 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2017
    Posts
    1
    Hi,



    Been following this thread with interest. I have taken Esandres data above and put it into an access database. Using Bulzies queries, I too am only getting the top 10 for 101.

    What is interesting is if you change it from Select top 10 - to Select top 16 - you would then get all 15 entries for 101 and the 1st entry to 102. Select top 20 would give you all 15 for 101 and 5 for 102 and so on.....

  2. #32
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,736
    Can you post the latest version of the database?

  3. #33
    Esandres is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2017
    Posts
    18
    Hi Orange

    Please find a snippet of the database attached below and the two queries:



    SELECT UXW.[Branch Code], UXW.Line, Sum(UXW.[Unknown Loss Value]) AS [SumOfUnknown Loss Value]
    FROM UXW
    GROUP BY UXW.[Branch Code], UXW.Line
    ORDER BY UXW.[Branch Code], Sum(UXW.[Unknown Loss Value]);



    SELECT qryUXWSorted.[Branch Code], qryUXWSorted.[SumOfUnknown Loss Value], qryUXWSorted.Line
    FROM qryUXWSorted
    WHERE (((qryUXWSorted.[SumOfUnknown Loss VALUE]) In (SELECT TOP 10 [SumOfUnknown Loss Value] FROM qryUXWSorted)))
    ORDER BY qryUXWSorted.[Branch Code], qryUXWSorted.[SumOfUnknown Loss Value];
    Attached Files Attached Files

  4. #34
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,477
    Yep its odd, I swear when i first set up a table and tested it it gave me 10 for Branch 1 and 10 for Branch 2, then it was giving me 10 for branch 1 and 7 for branch 2. Now it only gives 10 for branch 1 even though the option at the top of in the ribbon is set to All. Maybe something in that 2nd select statement is off?

  5. #35
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,477
    Well, if I go off the table only (no summing of UnknownLossValue), it seems to work. Try this code just on your table(change names to yours) and see if it returns 10 records per branch:

    SELECT BranchCode, UnknownLossValue, Line
    FROM UXW AS t
    WHERE (((t.UnknownLossValue) In (SELECT TOP 10 UnknownLossValue
    FROM UXW
    WHERE BranchCode = t.BranchCode
    ORDER BY BranchCode, UnknownLossValue
    )))
    ORDER BY t.BranchCode, t.UnknownLossValue;

    But when I try to sort and sum the UnknownLossValue and use that query and SumOfUnknownLossValue, it still counts the individual records in the 10 count. So if you had 2 records that summed up into 1 record, it would only return 9 records as it sums and groups those 2 records but for the query, it still treats them as individual records in counting the 10.

  6. #36
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,477
    If I saved that first query that Sums the UnknownLossValue as a table and then use that in the 2nd query, it seems to work. Maybe one of the other contributors can get it to work with 1 query or another way.

    Run this first one as a MakeTable query, to create table UXW2:

    SELECT UXW.BranchCode, Sum(UXW.UnknownLossValue) AS SumOfUnknownLossValue, UXW.Line INTO UXW2
    FROM UXW
    GROUP BY UXW.BranchCode, UXW.Line
    ORDER BY UXW.BranchCode, Sum(UXW.UnknownLossValue), UXW.Line;


    Then run this one that should give the top 10. Not sure why the query would not do correctly versus a table.

    SELECT t.BranchCode, t.SumOfUnknownLossValue, t.Line
    FROM UXW2 AS t
    WHERE (((t.SumOfUnknownLossValue) In (SELECT TOP 10 SumOfUnknownLossValue
    FROM UXW2
    WHERE BranchCode = t.BranchCode
    ORDER BY BranchCode, SumOfUnknownLossValue
    )))
    ORDER BY t.BranchCode, t.SumOfUnknownLossValue;

  7. #37
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,736
    This is my attempt.
    It shows a way of using SQL (Group, Sum and TOP)
    to display a Sum of the Lowest 10 values of each Group of records.
    The data in this sample is from the zip file in post 17.

    Try this sql
    Code:
    SELECT esandres.branch
        ,Sum(esandres.UnknownLossValue) AS SumOfUnknownLossValue
    FROM esandres
    WHERE esandres.UnknownLossValue IN (
            SELECT TOP 10 UnknownLossValue
            FROM esandres AS X
            WHERE x.branch = esandres.branch
            ORDER BY UnknownLossValue
            )
    GROUP BY esandres.branch
    Result based on your data file is
    branch SumOfUnknownLossValue
    101 -2592.939152
    102 -3504.529616

    I removed spaces from field names and imported into access as table Esandres

    Click image for larger version. 

Name:	Esandres.jpg 
Views:	8 
Size:	26.9 KB 
ID:	27791
    Last edited by orange; 03-09-2017 at 07:54 PM.

  8. #38
    Esandres is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2017
    Posts
    18
    Thank you guys,

    Just running them out now.

Page 3 of 3 FirstFirst 123
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query to Select Records and Alphabetize them
    By MarcieFess in forum Queries
    Replies: 2
    Last Post: 10-24-2013, 09:34 AM
  2. SELECT TOP 10 Query returns 12 records
    By Paul H in forum Queries
    Replies: 8
    Last Post: 09-11-2013, 03:38 PM
  3. Replies: 1
    Last Post: 04-02-2013, 08:41 AM
  4. Replies: 1
    Last Post: 07-10-2011, 11:20 PM
  5. Replies: 3
    Last Post: 01-04-2011, 07:06 PM

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