Select .. Having vs computer fields

Started by Francisco Reyesover 24 years ago4 messagesgeneral
Jump to latest
#1Francisco Reyes
lists@natserv.com

Can use use computed fields on the "having" part of select?
I have tried:
select a,b, count(*) as dups_count from some_table having dups_count > 1;

and it gives me the error "Attribute 'dups_count' not found"

#2Javier Dussaillant
ashkar@vrweb.cl
In reply to: Francisco Reyes (#1)
Re: Select .. Having vs computer fields

At 12:54 PM 12/12/2001 -0500, Francisco Reyes wrote:

Can use use computed fields on the "having" part of select?
I have tried:
select a,b, count(*) as dups_count from some_table having dups_count > 1;

and it gives me the error "Attribute 'dups_count' not found"

You have to GROUP BY in order to use HAVING. For example:

SELECT a,b, count(*) as dups_count FROM some_table GROUP BY a,b HAVING
dups_count > 1;

You should GROUP your rows by all the columns that are not count() or other
aggregate function (like max(), min(), sum(), etc).

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Javier Dussaillant G.
ashkar@vrweb.cl
Departamento de Desarrollo VRWEB ltda.
http://www.vrweb.cl

#3Francisco Reyes
lists@natserv.com
In reply to: Javier Dussaillant (#2)
Re: Select .. Having vs computer fields

On Wed, 12 Dec 2001, Javier Dussaillant wrote:

At 12:54 PM 12/12/2001 -0500, Francisco Reyes wrote:

Can use use computed fields on the "having" part of select?
I have tried:
select a,b, count(*) as dups_count from some_table having dups_count > 1;

and it gives me the error "Attribute 'dups_count' not found"

You have to GROUP BY in order to use HAVING. For example:

SELECT a,b, count(*) as dups_count FROM some_table GROUP BY a,b HAVING
dups_count > 1;

My mistake for not including it in the example. I did use Group By.

#4Antonio Fiol Bonnín
fiol@w3ping.com
In reply to: Francisco Reyes (#3)
Re: Select .. Having vs computer fields

Francisco Reyes wrote:

On Wed, 12 Dec 2001, Javier Dussaillant wrote:

At 12:54 PM 12/12/2001 -0500, Francisco Reyes wrote:

Can use use computed fields on the "having" part of select?
I have tried:
select a,b, count(*) as dups_count from some_table having dups_count > 1;

and it gives me the error "Attribute 'dups_count' not found"

You have to GROUP BY in order to use HAVING. For example:

SELECT a,b, count(*) as dups_count FROM some_table GROUP BY a,b HAVING
dups_count > 1;

My mistake for not including it in the example. I did use Group By.

Rewrite as

SELECT a,b, count(*) as dups_count FROM some_table GROUP BY a,b HAVING
count(*) > 1;

Antonio