Results 1 to 3 of 3
  1. #1
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15

    How to GroupBy and Count in Ranges Within a Query??

    I think it should use GroupBy and aggregate functions to do the following steps I require, however I just can’t get this to work:
    So say I have the table below but with more records
    Price


    Code:
    Cost Zone     Zip Code     Weight(lbs)  Price     Cost     Freight Value
    5                K0K          1209       0        135         289
    5                K0K          157         0        135         0
    5                K0L          1425       0         135        650
    5                K0L          1425       0         135        1999




    I wish to require a query stating that the count of “Freight Value” falls into one of the ranges

    Code:
    Zip Code    Range 0-500    Range 501-1000    Range 1001-2000
    k0K              2                     0            0
    K0L              0                     1            1

    How is this possible to do this ?? if you need me to explain with more detail let me know, thank you up ahead!

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Create an IIF statement to return a 1 if you range is met, and then change it to an Aggregate Query summing those calculations, i.e.
    Code:
    SELECT 
       MyTableName.[Zip Code], 
       Sum(IIf(([MyTableName]![Freight Value]>=0) And ([MyTableName]![Freight Value]<=500),1,0)) AS [Range_0-500], 
       Sum(IIf(([MyTableName]![Freight Value]>=501) And ([MyTableName]![Freight Value]<=1000),1,0)) AS [Range_501-1000], 
       Sum(IIf(([MyTableName]![Freight Value]>=1001) And ([MyTableName]![Freight Value]<=2000),1,0)) AS [Range_1001-2000]
    FROM MyTableName
    GROUP BY MyTableName.[Zip Code];

  3. #3
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15
    Quote Originally Posted by JoeM View Post
    Create an IIF statement to return a 1 if you range is met, and then change it to an Aggregate Query summing those calculations, i.e.
    Code:
    SELECT 
       MyTableName.[Zip Code], 
       Sum(IIf(([MyTableName]![Freight Value]>=0) And ([MyTableName]![Freight Value]<=500),1,0)) AS [Range_0-500], 
       Sum(IIf(([MyTableName]![Freight Value]>=501) And ([MyTableName]![Freight Value]<=1000),1,0)) AS [Range_501-1000], 
       Sum(IIf(([MyTableName]![Freight Value]>=1001) And ([MyTableName]![Freight Value]<=2000),1,0)) AS [Range_1001-2000]
    FROM MyTableName
    GROUP BY MyTableName.[Zip Code];
    LOL thats exactly what i ended up doing after playing around with all night thank you!

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

Similar Threads

  1. Query for gaps in date ranges
    By kagoodwin13 in forum Queries
    Replies: 6
    Last Post: 03-19-2012, 07:00 PM
  2. Count records by ranges
    By mjhopler in forum Access
    Replies: 2
    Last Post: 07-09-2011, 12:11 PM
  3. Replies: 2
    Last Post: 02-04-2011, 08:27 AM
  4. Query return for date ranges.
    By Desstro in forum Queries
    Replies: 2
    Last Post: 09-14-2010, 06:44 AM
  5. Select Query Using Different Date Ranges
    By Hemi426 in forum Queries
    Replies: 3
    Last Post: 07-26-2010, 01:06 PM

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