Results 1 to 3 of 3
  1. #1
    gap is offline Novice
    Windows Vista Access 2007
    Join Date
    Jul 2011
    Posts
    12

    nested query: avg, count, group by

    hi,
    I got a table as an example for my problem:

    theme /subtopic /partners

    a / fish / cornershop
    a / fish / supermarket
    a / vegetables / supermarket
    b / vegetables / cornershop
    b / fruits / cornershop
    c / fruits / cornershop
    c / fruits / cornershop
    c / fish / supermarket



    First, I want to count the number of partners fro each subtopic. Second, I want to get the Average Number of partners per subtopic for each theme.

    I started with this sql statement, but its not working:
    Code:
    SELECT theme, subtopic, Avg(t.NumberofPartners) AS Percentage_Partners
    FROM (SELECT COUNT(partners) As NumberofPartners
    FROM my_table
    GROUP BY subtopic)  AS t
    GROUP BY theme;
    Any Ideas?

  2. #2
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209

    nested query: avg, count, group by

    Try the following SQL:

    Code:
    SELECT my_Table.theme, 
    my_Table.subtopic, 
          Count(my_Table.subtopic) AS CountOfsubtopic,
          (SELECT count([theme]) as countoftheme from my_Table as myTable
     WHERE (my_Table.theme = myTable.theme) GROUP BY theme order by theme;) AS themecount, [countofsubtopic]/[themecount] AS [avg]
    FROM my_Table
    GROUP BY my_Table.theme, my_Table.subtopic;
    Ref. Link: Sub-Query in Query Column Expressions

  3. #3
    gap is offline Novice
    Windows Vista Access 2007
    Join Date
    Jul 2011
    Posts
    12
    thank you,
    right direction! but I want to get for each theme an average for the number of partners in the each subtopic.

    at least the table should look like:

    theme / average number of partners per subtopic

    a / [number]
    b
    c

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

Similar Threads

  1. Replies: 0
    Last Post: 04-25-2011, 07:58 PM
  2. GROUP and COUNT?
    By TheMoodyFiles in forum Queries
    Replies: 2
    Last Post: 02-10-2011, 05:50 PM
  3. SQL Count and Group By
    By Tyork in forum Programming
    Replies: 11
    Last Post: 01-24-2011, 09:06 AM
  4. Query to group and count field entries
    By newtoAccess in forum Queries
    Replies: 4
    Last Post: 11-29-2010, 09:19 AM
  5. Replies: 2
    Last Post: 11-18-2009, 06:49 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