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

From Nicole S. Weber
Subject count(*) - join question
Date
Msg-id 20011126221913.P22241-100000@tulip.atm.ox.ac.uk
Whole thread Raw
Responses Re: count(*) - join question  (Richard Poole <richard.poole@vi.net>)
List pgsql-sql
Hello.

I have set up a database to hold information regarding samples of insects
that I am counting.  I have two tables, one holds information about each
bug (sample number, type, length) and one contains information about each
sample (location, date etc.).  I have included the information on each
below.

I would like to SELECT the total number of bugs per sample, the sample ID,
the sample date and the sample code from the two tables to produce the
output shown here:

sample_id     |  sample_date   |  sample_code    |    count
------------------------------------------------------------   1         |  05/08/01      |   D15-1         |     5   2
       |  06/08/01      |   D20-1         |     8   .   .   .
 

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

I am quite new to this and I think I need something more complicated but I
just can't seem to figure it out.

Can anyone help?

Thanks.
Nicole
Table "animals"Attribute  |     Type      |
------------+---------------+animal_id  | integer       |sample_id  | integer       |type_id    | integer       |length
   | integer       |acomments  | character(50) |
 

Table "samples" Attribute    |     Type      |
---------------+---------------+-sample_id     | integer       |sample_date   | date          |sample_code   |
character(10)|time_id       | integer       |net_id        | integer       |sample_num    | integer       |duration
| numeric(5,2)  |count_date    | date          |scomments     | character(50) |
 





pgsql-sql by date:

Previous
From: "Josh Berkus"
Date:
Subject: Re: PL/pgSQL examples NOT involving functions
Next
From: Richard Poole
Date:
Subject: Re: count(*) - join question