Thread: count(*) and group by's...

count(*) and group by's...

From
Howie
Date:
I give up.  I'm trying to get a count of one table joining with another,
but Postgres ( 6.3.2 ) doesnt like me.

SELECT types.type,hosts.hostname,types.batch,count(codes.code) FROM
hosts,types,codes WHERE hosts.client=3 AND hosts.client=types.client AND
types.type=codes.type GROUP BY types.type ORDER BY types.batch;

returns:

ERROR:  parser: illegal use of aggregates or non-group column in target
list

in fact, trying to do any join across types and codes results in an
illegal use/non-group column.  types.type is primary,
codes.type+codes.code is primary.

---
Howie <caffeine@toodarkpark.org>   URL: http://www.toodarkpark.org
        "Oh my god, they killed init!  YOU BASTARDS!"


Re: [SQL] count(*) and group by's...

From
David Hartwig
Date:
SELECT types.type, hosts.hostname, types.batch, count(codes.code)
        FROM hosts,types,codes
WHERE hosts.client=3 AND
    hosts.client=types.client AND
    types.type=codes.type
GROUP BY types.type, hosts.hostname, types.batch
--                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ORDER BY types.batch;

-- All columns in the target (projection) must be either an aggregate
function or grouped by


Howie wrote:

> I give up.  I'm trying to get a count of one table joining with another,
> but Postgres ( 6.3.2 ) doesnt like me.
>
> SELECT types.type,hosts.hostname,types.batch,count(codes.code) FROM
> hosts,types,codes WHERE hosts.client=3 AND hosts.client=types.client AND
> types.type=codes.type GROUP BY types.type ORDER BY types.batch;
>
> returns:
>
> ERROR:  parser: illegal use of aggregates or non-group column in target
> list
>
> in fact, trying to do any join across types and codes results in an
> illegal use/non-group column.  types.type is primary,
> codes.type+codes.code is primary.
>
> ---
> Howie <caffeine@toodarkpark.org>   URL: http://www.toodarkpark.org
>         "Oh my god, they killed init!  YOU BASTARDS!"