Re: Using aggregates to get sums and partial sums in one query - Mailing list pgsql-sql

From David G Johnston
Subject Re: Using aggregates to get sums and partial sums in one query
Date
Msg-id 1415636350305-5826346.post@n5.nabble.com
Whole thread Raw
In response to Re: Using aggregates to get sums and partial sums in one query  (Igor Neyman <ineyman@perceptron.com>)
Responses Re: Using aggregates to get sums and partial sums in one query
List pgsql-sql
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.



pgsql-sql by date:

Previous
From: Igor Neyman
Date:
Subject: Re: Using aggregates to get sums and partial sums in one query
Next
From: Andreas Joseph Krogh
Date:
Subject: Re: Using aggregates to get sums and partial sums in one query