Results 1 to 6 of 6
  1. #1
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419

    SELECT & GROUP BY Clause

    Hi all,



    Does all the fields in the SELECT statement needs to be in the GROUP BY clause regardless of JOINs or not ?

    I always thought that what you have in the SELECT, you also need them in the GROUP BY for it to work.

    For example:

    SELECT a, b, c, d
    GROUP BY a, b, c, d
    Or can you have fields in the SELECT statement, and in the GROUP BY you have what you have in the SELECT statement and then some extra in the GROUP BY ?

    I always thought that what you had in the SELECT, you also need them in the GROUP BY for it to work (not extra).
    For example:

    SELECT a, b, c, d
    GROUP BY a, b, c, d, e, f

    In these two scenario, does JOINs make a difference ?

    Thank you !!!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Group By is only needed if you are not summing it.

    Group By Name, but SUM(Amt) to get the sum for each Name. (basically its a SHOW this field)

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Lots of samples and verbiage here.

  4. #4
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    Group By is only needed if you are not summing it.

    Group By Name, but SUM(Amt) to get the sum for each Name. (basically its a SHOW this field)
    I believe GROUP BY is use when you are either counting or summing ! When you are grouping them together,
    so you de-dup them.

    Thanks Orange, I am looking to your samples.

  5. #5
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    Lots of samples and verbiage here.
    Orange, I see that example, I understand them.

    In both those example, the GROUP BY was use for the field and not the sum or counts, hence, the
    SELECT statement has more fields than the GROUP BY clause.

    I have an example where the GROUP BY clause field are more than the SELECT statement. I just think is weird and how is that possible, that is
    why I ask the questions on top, and hence this thread because that doesn't make sense to me.

    From what I know of GROUP BY is that in the GROUP BY statement any field that it reference should be in the SELECT statement with the exception
    of either count or sum of.

  6. #6
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    Look at this logic, this is just an example, you see the GROUP BY there are a total of 6 fields, while the SELECT is only 4 fields, none of the SELECT are either SUM or COUNT.
    Why is this working ? I believe it shouldn't, I believe the number of field of SELECT you have is the number of GROUP BY you should have with the exception to the SELECT having SUM or COUNT.
    However in this case is not, we have more GROUP BY fields than we have SELECT fields.

    Code:
    INSERT INTO [tableA] (A, I, C, R)
    SELECT tableB.A, tableB.B, tableC.D, tableB.E
    FROM (tableB LEFT JOIN tableE ON tableB.A = tableE.C) LEFT JOIN tableC ON tableB.B = tableC.B
    GROUP BY tableB.A, tableB.B, tableC.D, tableB.E, tableC.B,  table.C

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

Similar Threads

  1. Syntax error on VB Select Where Clause
    By FrustratedAlso in forum Programming
    Replies: 3
    Last Post: 01-30-2014, 02:57 AM
  2. GROUP by yet SELECT fields not within GROUP BY
    By johnseito in forum Access
    Replies: 25
    Last Post: 11-03-2013, 10:20 PM
  3. Replies: 6
    Last Post: 07-24-2012, 03:02 PM
  4. Use of COLLATE statement in SELECT clause
    By zurek in forum Queries
    Replies: 7
    Last Post: 03-16-2011, 06:46 AM
  5. Nested Select Statements in FROM clause
    By neonslip in forum Queries
    Replies: 11
    Last Post: 11-03-2010, 10:58 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