Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Location
    Brandıs nad Labem - Czech Republic
    Posts
    36

    Question SQL query with SUM, INNER JOIN and GROUP BY

    Hi, I'm stuck with a query that worked fine in mySQL. Would look somewhat like this:



    3 tables "A", "B", "C" with collumns
    A: "id"
    B: "id", "a_id" - a foreign key to the "A" table
    C: "id", "b_id" - a foreign key to the "B" table, "qty" - some data to SUM()

    So the relationship is "A --1:n-- B --1:n-- C"

    Code:
    SELECT A.id, B.id, COUNT(C.id) AS count_c, SUM(C.qty) AS qty_c
    FROM (A INNER JOIN B ON A.id = B.a_id) INNER JOIN C ON B.id = C.b_id
    GROUP BY B.id
    I simplified it a lot, hope that makes no difference (instead of selecting ID's I select useful information from A and B). My Access does not speak english so I can't really say the exact error message. Something like "the a.id is not part of the aggregation function".

    Anyone knows what the heck it wants? The query seems pretty understandable to me

  2. #2
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    add a.id in group by list. every fields(except aggregate fields) in select list must in group by list.

    SELECT A.id, B.id, COUNT(C.id) AS count_c, SUM(C.qty) AS qty_c
    FROM (A INNER JOIN B ON A.id = B.a_id) INNER JOIN C ON B.id = C.b_id
    GROUP BY B.id, A.id

  3. #3
    Join Date
    Aug 2010
    Location
    Brandıs nad Labem - Czech Republic
    Posts
    36

    Smile

    OK, thank you very much. It works.

    I changed the SELECT part to show data instead of IDs and put them into the GROUP BY. I left the IDs there too (in GROUP BY), because data needn't be unique.

    It took me however longer, 'cause little typos are hard to find with the error messages Access gives.

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

Similar Threads

  1. Join Query?
    By KWarzala in forum Forms
    Replies: 5
    Last Post: 07-29-2014, 02:44 PM
  2. inner join in query
    By Hrcko in forum Access
    Replies: 1
    Last Post: 02-01-2010, 05:54 PM
  3. Stuck on Join Query
    By Pimped in forum Queries
    Replies: 1
    Last Post: 10-26-2009, 10:54 AM
  4. How does Access guess join field in query?
    By bar tomas in forum Database Design
    Replies: 1
    Last Post: 05-27-2009, 05:56 PM
  5. Replies: 1
    Last Post: 09-19-2006, 11:07 AM

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