pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christoph Haller <ch@rodos.fzk.de>
To: pgsql-sql@postgresql.org
Cc: martinkuria@hotmail.com
Subject: Re: sub query
Date: Wed, 17 Sep 2003 10:54:49 +0200
Message-ID: <3F682159.547F4840@rodos.fzk.de> (raw)

>
> > Hi I have this problem, when I try to run this query:
> >
> > SELECT MAX(d), host_position FROM (SELECT host_position,
> > COUNT(host_position) as d FROM sss_host GROUP BY host_position) as
e;
> >
> > am getting and ERROR: Attribute e.host_position must be GROUPed or
> used in
> > an aggregate function.
> >
> > Please to advice what could be the problem and how can I rewrite it
to
> work
> > thanks in advance.
> >
> As the error message says: e.host_position must be GROUPed
>
> so (supposing you want a one row result showing the maximum count)
>
> SELECT MAX(e.d), e.host_position FROM (SELECT host_position,
> COUNT(host_position) as d FROM sss_host GROUP BY host_position) as e
> GROUP BY e.host_position ORDER BY 1 LIMIT 1;
>
> should match your intentions.
>
Just thought about another (less complex) way:

SELECT COUNT(host_position), host_position FROM
sss_host GROUP BY host_position ORDER BY 1 DESC LIMIT 1;

Regards, Christoph





view thread (8+ messages)  latest in thread

Message-ID: <3F682159.547F4840@rodos.fzk.de>
Permalink:  ../3F682159.547F4840@rodos.fzk.de/
Also on:    postgresql.org/message-id/3F682159.547F4840@rodos.fzk.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ch@rodos.fzk.de, martinkuria@hotmail.com
  Subject: Re: sub query
  In-Reply-To: <3F682159.547F4840@rodos.fzk.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox