A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

HAVING clause



 
 
Thread Tools Display Modes
  #1  
Old December 31st, 2009, 03:31 PM posted to microsoft.public.access.queries
Rebecca
external usenet poster
 
Posts: 284
Default HAVING clause

Having trouble with HAVING clause.

Say my table shows families with one row for each kid, labeling that kid's
gender
So Jones family has 2 girls, 1 boy; Smith family has 2 boys, etc.

Jones / girl
Jones / girl
Jones /boy
Smith /boy
Smith / boy
Sanchez / girl
Sanchez /girl
Chang / boy

I want to ONLY get the families that DON'T have ANY girls. So my query
result should be Smith & Chang.

I did: HAVING (gender "girl")

But get an error message about it not being part of aggregate function.
Is there a way to make this work?

Note: I already did the query a different way, making girl=1 and boy =0,
instead of girl/boy word labels, and then just selected sum 1. (i.e., no
girls) But I still want to figure out how to use the HAVING clause in this
instance, if it's possible.

Thanks.


  #2  
Old December 31st, 2009, 03:50 PM posted to microsoft.public.access.queries
Allen Browne
external usenet poster
 
Posts: 11,706
Default HAVING clause

Use a subquery:
http://allenbrowne.com/subquery-01.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


"Rebecca" wrote in message
...
Having trouble with HAVING clause.

Say my table shows families with one row for each kid, labeling that kid's
gender
So Jones family has 2 girls, 1 boy; Smith family has 2 boys, etc.

Jones / girl
Jones / girl
Jones /boy
Smith /boy
Smith / boy
Sanchez / girl
Sanchez /girl
Chang / boy

I want to ONLY get the families that DON'T have ANY girls. So my query
result should be Smith & Chang.

I did: HAVING (gender "girl")

But get an error message about it not being part of aggregate function.
Is there a way to make this work?

Note: I already did the query a different way, making girl=1 and boy =0,
instead of girl/boy word labels, and then just selected sum 1. (i.e., no
girls) But I still want to figure out how to use the HAVING clause in
this
instance, if it's possible.

Thanks.


  #3  
Old December 31st, 2009, 06:21 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default HAVING clause


SELECT Family
FROM Table
GROUP BY Family
HAVING Sum(Gender="Girl") = 0

Any comparisons in a HAVING clause must include one of the aggregate functions
(or be in the group by clause). As noted elsewhere, you could use a sub-query
in a WHERE clause (not a HAVING clause) to limit the records returned.



John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Rebecca wrote:
Having trouble with HAVING clause.

Say my table shows families with one row for each kid, labeling that kid's
gender
So Jones family has 2 girls, 1 boy; Smith family has 2 boys, etc.

Jones / girl
Jones / girl
Jones /boy
Smith /boy
Smith / boy
Sanchez / girl
Sanchez /girl
Chang / boy

I want to ONLY get the families that DON'T have ANY girls. So my query
result should be Smith & Chang.

I did: HAVING (gender "girl")

But get an error message about it not being part of aggregate function.
Is there a way to make this work?

Note: I already did the query a different way, making girl=1 and boy =0,
instead of girl/boy word labels, and then just selected sum 1. (i.e., no
girls) But I still want to figure out how to use the HAVING clause in this
instance, if it's possible.

Thanks.


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 01:37 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.