Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XnrgW-0003ay-VP for pgsql-sql@arkaria.postgresql.org; Mon, 10 Nov 2014 16:19:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XnrgV-0005Ca-Nc for pgsql-sql@arkaria.postgresql.org; Mon, 10 Nov 2014 16:19:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XnrgT-0005CQ-Op for pgsql-sql@postgresql.org; Mon, 10 Nov 2014 16:19:17 +0000 Received: from [162.253.133.43] (helo=mwork.nabble.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XnrgO-0008L3-SY for pgsql-sql@postgresql.org; Mon, 10 Nov 2014 16:19:16 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id 5577098073A for ; Mon, 10 Nov 2014 08:19:11 -0800 (PST) Date: Mon, 10 Nov 2014 09:19:10 -0700 (MST) From: David G Johnston To: pgsql-sql@postgresql.org Message-ID: <1415636350305-5826346.post@n5.nabble.com> In-Reply-To: References: Subject: Re: Using aggregates to get sums and partial sums in one query MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 162.253.133.43 (failed) X-Pg-Spam-Score: 1.6 (+) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Igor Neyman wrote > select p.name, sum(distinct m.years_experience) as years_exp_in_project, This is wrong on its face; two different people with the same experience will result in under-counting You need to figure out some way for the DISTINCT to include a personID and then just sum up the year_experience for each individual. My initial thought was to use "ARRAY_AGG(DISTINCT composite_type)" to construct the unique dataset then pass the result through a custom function that would unnest(...) that array, pull out the years, sum them, and return the sum. > count(distinct (m.id, m.role)) Not technically a true role count if two people share the same role - it is unclear from the query what constraints the problem domain imposes. This can be a solution for this column. I don't really see how using a couple of CTEs to build up summaries for each table and then joining them together in an outer query is problematic. TBH it would probably be easier to maintain than one single super query with a bunch of distinct aggregates; and I doubt there would be much if any performance hit. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Using-aggregates-to-get-sums-and-partial-sums-in-one-query-tp5826157p5826346.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql