Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i3kTz-0001Sv-4j for pgsql-general@arkaria.postgresql.org; Fri, 30 Aug 2019 17:14:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i3kTx-00005E-1p for pgsql-general@arkaria.postgresql.org; Fri, 30 Aug 2019 17:14:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i3kTw-000057-OC for pgsql-general@lists.postgresql.org; Fri, 30 Aug 2019 17:14:40 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i3kTq-0002i7-BE for pgsql-general@postgresql.org; Fri, 30 Aug 2019 17:14:40 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 436AD9939A for ; Fri, 30 Aug 2019 10:14:31 -0700 (PDT) Date: Fri, 30 Aug 2019 10:14:31 -0700 (PDT) From: Rich Shepard Reply-To: Rich Shepard To: pgsql-general@postgresql.org Subject: Aggregate functions on groups Message-ID: User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Tables hold data on fish counts by stream name, species, and (unreported) collection dates. I'm trying to write a query that returns the total number of each species in each stream. The latest attempt is (lines wrapped by alpine; submitted as one line): \copy (select f.stream_tribs, f.count_value, sum(f.count_value), i.common_name, i.sci_name from fish_counts as f, itis as i where f.stream_tribs like '%Nehalem River%' group by f.stream_tribs, i.common_name, i.sci_name, f.count_value order by f.stream_tribs, i.common_name, i.sci_name, f.count_value) to '/home/rshepard/projects/oregon/mohler_sand/data/fish/fishes.dat'; The returned set starts this way: Anderson Creek trib to Nehalem River 0 0 Black crappie Pomoxis nigromaculatus Anderson Creek trib to Nehalem River 3 3 Black crappie Pomoxis nigromaculatus Anderson Creek trib to Nehalem River 0 0 Bluegill Lepomis macrochirus Anderson Creek trib to Nehalem River 3 3 Bluegill Lepomis macrochirus Anderson Creek trib to Nehalem River 0 0 Brook trout Salvelinus fontinalis Anderson Creek trib to Nehalem River 3 3 Brook trout Salvelinus fontinalis Anderson Creek trib to Nehalem River 0 0 Brown bullhead Ameiurus nebulosus Anderson Creek trib to Nehalem River 3 3 Brown bullhead Ameiurus nebulosus What I want returned would look like this: Anderson Creek trib to Nehalem River Black crappie Pomoxis nigromaculatus 3 Anderson Creek trib to Nehalem River Bluegill Lepomis macrochirus 3 Anderson Creek trib to Nehalem River Brook trout Salvelinus fontinalis 3 Anderson Creek trib to Nehalem River Brown bullhead Ameiurus nebulosus 3 I've read the manual yet must have not seen the section explaining how to apply aggregate functions to groups. Thanks in advance, Rich