From: Nicole S. Weber <nweber@ic.ac.uk>
To: pgsql-sql@postgresql.org
Subject: count(*) - join question
Date: Mon, 26 Nov 2001 23:03:56 +0000 (GMT)
Message-ID: <20011126221913.P22241-100000@tulip.atm.ox.ac.uk> (raw)
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) |
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: nweber@ic.ac.uk
Subject: Re: count(*) - join question
In-Reply-To: <20011126221913.P22241-100000@tulip.atm.ox.ac.uk>
References: <20011126221913.P22241-100000@tulip.atm.ox.ac.uk>
* 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