Results 1 to 4 of 4
  1. #1
    JSvar is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    13

    Query to generate columns determined by the count of a field within different ranges

    I am trying to create a query that uses the count of a specific field and generates columns for different range values. For Example.

    Count(terminal_city_locations)

    City Count >1 and <6 Count >5 and <11 Count >10 and <16

    London 3
    Munich 6
    Paris 13

    I can get the count for each city, but am struggling with coming up with a way to format the results into a table that reflects the information. When I try to use multiple Counts with different ranges for the value I get no results since no city satisfies more than one of the conditions.



    Is something like this possible?

  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
    This could require several queries.

    Need a field that will assign a group identifier.

    Build a query with the count query:

    SELECT terminal_city_locations, IIf(CountCity<6, ">1 and <5", IIf(CountCity<11, ">5 and <11", IIf(CountCity<16, ">10 and <16", ">16"))) AS CountGroup FROM CountQuery;

    Then take that query and build a CROSSTAB with it.
    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
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    This can be done easily using a crosstab query, but you probably just need a simple query with a "spreader" feature.
    Assuming your original table looked like this:
    Code:
    tblTerminals
      TermID, 
      CityName,
      TerminalName
    
    query1:
       SELECT
          Cityname,
          Count(TerminalName) As TermCount
       FROM 
          tblTerminals
       GROUP BY CityName;
    Then your spreader takes the results of that query and spreads it by size category:
    Code:
    SELECT 
      CityName,
      IIF(TermCount>0 AND TermCount <= 5, TermCount,0) As TermCount0105,
      IIF(TermCount>5 AND TermCount <= 10, TermCount,0) As TermCount0610,
      IIF(TermCount>0 AND TermCount <= 15, TermCount,0) As TermCount1115,
      IIF(TermCount>0 AND TermCount <= 20, TermCount,0) As TermCount1620,
      IIF(TermCount>20, TermCount,0) As TermCount2199,
    FROM 
      (SELECT
      Cityname,
      Count(TerminalName) As TermCount
      FROM tblTerminals
      GROUP BY CityName);
    That's easy enough, right?

  4. #4
    JSvar is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    13
    This work great. Thank you very much.

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

Similar Threads

  1. Replies: 4
    Last Post: 10-26-2013, 02:04 PM
  2. Replies: 2
    Last Post: 07-19-2012, 06:23 AM
  3. Count records by ranges
    By mjhopler in forum Access
    Replies: 2
    Last Post: 07-09-2011, 12:11 PM
  4. Generate datasheet columns on the fly?
    By kman42 in forum Programming
    Replies: 0
    Last Post: 03-11-2011, 03:52 AM
  5. Replies: 1
    Last Post: 01-24-2011, 03:03 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