Re: count(*) - join question

From: Richard Poole <richard(dot)poole(at)vi(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: count(*) - join question
Date: 2001-11-27 14:25:30
Message-ID: 20011127142530.C24565@office.vi.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Aasmund Midttun Godal 2001-11-27 19:53:51 Re: PL/pgSQL examples NOT involving functions
Previous Message Gyorgy Molnar 2001-11-27 14:11:18 Queue in SQL