Re: count(*) - join question - Mailing list pgsql-sql

From Richard Poole
Subject Re: count(*) - join question
Date
Msg-id 20011127142530.C24565@office.vi.net
Whole thread Raw
In response to count(*) - join question  ("Nicole S. Weber" <nweber@ic.ac.uk>)
List pgsql-sql
On Mon, Nov 26, 2001 at 11:03:56PM +0000, Nicole S. Weber wrote:
> 
> I have tried to use a join on the sample_id in combination with
> COUNT(*)/GROUP BY sample_id:
> 
> SELECT animals.sample_id, samples.sample_date, samples.sample_code,
> count(*)
>   FROM animals, samples
>   WHERE samples.sample_id = animals.sample_id
>   GROUP BY animals.sample_id;
> 
> Alas, this returns:
> 
> ERROR:  Attribute samples.sample_Dadate must be GROUPed or used in an
> aggregate function

You need to GROUP BY all the columns that you're not counting the
different occurrences of:

SELECT animals.sample_id, samples.sample_date, samples.sample_code, count(*)
FROM animals, samples
WHERE samples.sample_id = animals.sample_id
GROUP BY animals.sample_id, samples.sample_date, samples.sample_code;

Richard


pgsql-sql by date:

Previous
From: "Nicole S. Weber"
Date:
Subject: count(*) - join question
Next
From: "Aasmund Midttun Godal"
Date:
Subject: Re: PL/pgSQL examples NOT involving functions