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

    GROUP BY with WHERE

    Anyone know why this code didn't work, but the code below it works fine?
    I have seen WHERE clause works with GROUP BY, just not this one, not sure why.

    Code:
    SELECT seller, iid, startTIME , count(*) as cntBID
    FROM bid 
    WHERE count(*) = 1
    GROUP BY seller, iid, startTIME

    Code:
    SELECT seller, iid, startTIME , count(*) as cntBID
    FROM bid 
    GROUP BY seller, iid, startTIME
    HAVING count(*) = 1


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    The WHERE clause is applied before the aggregating, the HAVING clause after, so that criteria would have to be in the HAVING clause.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    Ok, pbaldy,

    Thanks I see what you are saying, yet this code below works.
    I believe the below code doesn't rely on GROUP BY bc no aggregation is needed
    as oppose to the one with WHERE count(*) =1 is, and GROUP BY has to execute first.

    Code:
    SELECT seller, iid, startTIME , count(*) as cntBID
    FROM bid 
    WHERE seller = 7
    GROUP BY seller, iid, startTIME

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Because restricting by seller is something you want done before the aggregation. If you wanted to combine the two, you'd still need a HAVING clause for the count = 1. The seller = 7 would actually work in either, but is more efficient as a WHERE.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    Ok, got it. :-)

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

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2013, 02:34 PM
  2. Replies: 0
    Last Post: 02-25-2013, 04:43 PM
  3. Replies: 2
    Last Post: 06-21-2012, 07:40 PM
  4. Group title carrying forward to next group
    By Amandasr12 in forum Reports
    Replies: 3
    Last Post: 06-06-2012, 02:13 PM
  5. columns for group detail but not group header?
    By Coolpapabell in forum Reports
    Replies: 0
    Last Post: 08-21-2009, 08:53 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