GROUP BY / HAVING - am I being thick?
От | Andrew C.R. Martin |
---|---|
Тема | GROUP BY / HAVING - am I being thick? |
Дата | |
Msg-id | 0004131544420E.29884@sapc13.rdg.ac.uk обсуждение исходный текст |
Ответы |
Re: GROUP BY / HAVING - am I being thick?
|
Список | 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 1286 f 1286 f 4286 t 13286 t 37286 t 1287 f 3287 f 3287 f 4287 f 1288 f 2288 f 3288 f 4288 f 2289 f 1289 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 5287 11288 11289 1 ------------------------ I can do this using a temporary table (or equally using a view): SELECT codon, nexamples INTO summary_tmp FROMsummary 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.c.r.martin@reading.ac.uk (work) Lecturer in Bioinformatics andrew@stagleys.demon.co.uk (home) University of Reading Tel.: +44 (0)118 987 5123x7022 Fax: +44 (0)118 931 0180
В списке pgsql-sql по дате отправления: