GROUP BY / HAVING - am I being thick?

From: "Andrew C(dot)R(dot) Martin" <a(dot)c(dot)r(dot)martin(at)reading(dot)ac(dot)uk>
To: pgsql-sql(at)postgresql(dot)org
Subject: GROUP BY / HAVING - am I being thick?
Date: 2000-04-13 14:38:27
Message-ID: 0004131544420E.29884@sapc13.rdg.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


I have a question about GROUP BY / HAVING. It seems I can't do
something I *think* I should be able to do!

Give the following table called 'summary':
------------------------
(int) (bool) (int)
codon expl nexamples
------------------------
286 t 1
286 f 1
286 f 4
286 t 13
286 t 37
286 t 1
287 f 3
287 f 3
287 f 4
287 f 1
288 f 2
288 f 3
288 f 4
288 f 2
289 f 1
289 t 3
------------------------

For each codon number, I want to obtain sum(nexamples) where expl = 'f';

i.e. I want to get the result:
------------------------
(int) (int)
codon sum
------------------------
286 5
287 11
288 11
289 1
------------------------

I can do this using a temporary table (or equally using a view):
SELECT codon, nexamples INTO summary_tmp
FROM summary
WHERE expl = 'f';
SELECT codon, sum(nexamples)
FROM summary_tmp
GROUP BY codon;
DROP TABLE summary_tmp;

but I thought I should be able to do it using GROUP BY and HAVING.
The following looks as though it should work according to "The
Practical SQL Handbook" (Bowman, Emerson & Darnovsky)

SELECT codon, sum(nexamples)
FROM summary
WHERE expl = 'f'
GROUP BY codon
HAVING expl = 'f';

I've tried various contortions of putting expl in the SELECT clause
and in the GROUP BY clause, but nothing seems to work. The docs say that
PostgreSQL requires the HAVING clause "unambiguously reference a grouping
column" whatever that means - I tried putting expl in the GROUP BY, but it
didn't help.

Any suggestions gratefully received!

Thanks,

Andrew

--
Dr. Andrew C.R. Martin EMail: a(dot)c(dot)r(dot)martin(at)reading(dot)ac(dot)uk (work)
Lecturer in Bioinformatics andrew(at)stagleys(dot)demon(dot)co(dot)uk (home)
University of Reading
Tel.: +44 (0)118 987 5123x7022 Fax: +44 (0)118 931 0180

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Moray McConnachie 2000-04-13 14:51:35 Re: Subtracting from a date
Previous Message Andy Lewis 2000-04-13 14:06:09 Re: Subtracting from a date