question re. count, group by, and having - Mailing list pgsql-sql

From Rick Schumeyer
Subject question re. count, group by, and having
Date
Msg-id 000b01c5ce62$514db090$0300a8c0@dell8200
Whole thread Raw
Responses Re: question re. count, group by, and having
Re: question re. count, group by, and having
Re: question re. count, group by, and having
List pgsql-sql
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">The following query returns an error (“column c does not exist”) in pg 8.0.3:</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">(The column ‘state’ is the two letter abbreviation for a </span></font><font face="Arial"
size="2"><spanstyle="font-size:10.0pt;font-family:Arial">US</span></font><font face="Arial" size="2"><span
style="font-size:10.0pt;font-family:Arial">state)</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">-- get the number of rows for each state; list in descending order; include only states with at
least6 rows</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial">select state, count(state) as c from t group by state having c > 5 order by c desc; -- gives
error</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">If I leave the having clause out, I get the expected results:</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">select state, count(state) as c from t group by state order by c desc; -- this works</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Is this a bug or a feature?  I’m not sure why I can use ‘c’ in the order by clause but not the
havingclause.  pg is much happier with the full “having count(state) > 5”.  Will this cause count to be evaluated
twice?</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">If it matters, state is varchar(2).</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial"> </span></font></div>

pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: SEVEN cross joins?!?!?
Next
From: Sean Davis
Date:
Subject: Re: question re. count, group by, and having