agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Andreas Joseph Krogh <andreas@visena.com>
To: pgsql-sql@postgresql.org
Subject: Re: Using aggregates to get sums and partial sums in one query
Date: Mon, 10 Nov 2014 19:52:42 +0100 (CET)
Message-ID: <VisenaEmail.c0.76488173e5def8d5.1499b0937b2@tc7-visena> (raw)
In-Reply-To: <1415636350305-5826346.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
På mandag 10. november 2014 kl. 17:19:10, skrev David G Johnston <
david.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>>: 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. Using CTEs is what I am doing, just wondered if there is
any other viable solution. In reality the query uses paging with "total count"
(count(*) with the same FROM-clause) because the dataset is large. The query is
dynamically built up and counting totals with the "group by" required (when not
using CTEs) in main-query easily messes up the total-count. -- Andreas Joseph
Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963 andreas@visena.com
<mailto:andreas@visena.com> www.visena.com <https://www.visena.com;
<https://www.visena.com;
view thread (4+ messages)
Message-ID: <VisenaEmail.c0.76488173e5def8d5.1499b0937b2@tc7-visena>
Permalink: ../VisenaEmail.c0.76488173e5def8d5.1499b0937b2@tc7-visena/
Also on: postgresql.org/message-id/VisenaEmail.c0.76488173e5def8d5.1499b0937b2@tc7-visena
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: andreas@visena.com
Subject: Re: Using aggregates to get sums and partial sums in one query
In-Reply-To: <VisenaEmail.c0.76488173e5def8d5.1499b0937b2@tc7-visena>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox