pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: stan <stanb@panix.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: A GROUP BY question
Date: Tue, 13 Aug 2019 14:59:22 +0200
Message-ID: <CDB18AF4-8C63-4D67-94D3-0895049F1F52@gmail.com> (raw)
In-Reply-To: <20190813111018.GA19289@panix.com>
References: <20190813095403.GA2013@panix.com>
	<20190813111018.GA19289@panix.com>


> On 13 Aug 2019, at 13:10, stan <stanb@panix.com> wrote:
> 
> select 
> 	project.proj_no ,

Removed columns that get in the way of your desired result. You can’t have both details and the sum over them in a meaningful way.

> 	SUM (rate.rate * task_instance.hours) 
> from 
> 	task_instance
> join rate on 
> 	rate.employee_key = task_instance.employee_key
> 	AND
> 	rate.work_type_key = task_instance.work_type_key

(break)

> inner join employee on
> 	rate.employee_key = employee.employee_key
> inner join work_type on
> 	rate.work_type_key = work_type.work_type_key

These are now probably redundant, you don’t need them unless they filter your results.

> inner join project on
> 	project.project_key = task_instance.project_key

And this JOIN could be dropped if project_key and proj_no weren’t different fields. If both are unique in project, you could drop one of them and keep the same functionality with fewer joins. That said, in the “war” between surrogate and natural keys I’m on the natural keys side. Clearly, not everyone agrees on that.

> GROUP BY 
> 	project.project_key ,

Same columns removed here too.

> ORDER BY 
> 	project.proj_no 
> 	;

That should give you the total cost for each project.

You could get the same result repeated per employee and per work type as you tried originally, by putting the above revised query as a subquery and joining that back into the full query in the place of your project-related tables (add the project_key so you have something to join against).

The repeated sum risks getting multiplied in the final output though, especially if unaware people will be putting the results in an Excel sheet or something. From experience, that either results in people reporting the wrong financial results (several orders too high) or blaming your query.

Regards,

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






view thread (6+ messages)  latest in thread

Message-ID: <CDB18AF4-8C63-4D67-94D3-0895049F1F52@gmail.com>
Permalink:  ../CDB18AF4-8C63-4D67-94D3-0895049F1F52@gmail.com/
Also on:    postgresql.org/message-id/CDB18AF4-8C63-4D67-94D3-0895049F1F52@gmail.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, stanb@panix.com
  Subject: Re: A GROUP BY question
  In-Reply-To: <CDB18AF4-8C63-4D67-94D3-0895049F1F52@gmail.com>
  References: <20190813095403.GA2013@panix.com> <20190813111018.GA19289@panix.com> <CDB18AF4-8C63-4D67-94D3-0895049F1F52@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox