pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jan Kohnert <nospam001-lists@jan-kohnert.de>
To: pgsql-general@lists.postgresql.org
Subject: Re: A GROUP BY question
Date: Tue, 13 Aug 2019 15:07:46 +0200
Message-ID: <1638919.PXEyjZ9AVP@kohnert-n4> (raw)
In-Reply-To: <20190813111018.GA19289@panix.com>
References: <20190813095403.GA2013@panix.com>
	<20190813111018.GA19289@panix.com>

Hi Stan,

Am Dienstag, 13. August 2019, 13:10:18 CEST schrieb stan:
> Maybe I have a basic misunderstanding. What I am trying to get is a total
> cost for each project. This would be calculated by multiplying rate and
> hours for each row, on a per project base, and then summing all of th
> products of this multiplication.

First of all, complex views including many joins, group by, having etc usually tend to 
show bad performance on large dataset if used for anything else than simple select from 
... statement. So at least my personal experience.

> CREATE OR REPLACE view tasks_view as
> select
> 	project.proj_no ,
> 	employee.first_name ,
> 	employee.last_name ,
> 	employee.id ,
> 	task_instance.hours ,
> 	work_type.type,
> 	work_type.descrip,
> 	rate.rate,
> 	employee.hourly_rate ,
> 	rate.rate * task_instance.hours as result ,
--------------------^
this gives you the product *per line*

> 	SUM (rate.rate * task_instance.hours)
--------------------^
this gives you *sum of all products over all grouped lines*

> from
> 	task_instance
> join rate on
> 	rate.employee_key = task_instance.employee_key
> 	AND
> 	rate.work_type_key = task_instance.work_type_key
> inner join employee on
> 	rate.employee_key = employee.employee_key
> inner join work_type on
> 	rate.work_type_key = work_type.work_type_key
> inner join project on
> 	project.project_key = task_instance.project_key
> GROUP BY
> 	project.project_key ,
> 	employee.first_name ,
> 	employee.last_name ,
> 	employee.id ,
> 	task_instance.hours ,
> 	work_type.type,
> 	work_type.descrip,
> 	rate.rate,
> 	employee.hourly_rate
---------------------^

You group by the columns you use in the sum, so you will get no sum at all, but the 
product *per line* as selected just before the sum

> ORDER BY
> 	project.proj_no
> 	;

You will have to find out if you really need to group by some lines, and take a sum over 
those lines or need the product (rate.rate * task_instance.hours) per line.

-- 
Best regards 
Jan

view thread (6+ messages)  latest in thread

Message-ID: <1638919.PXEyjZ9AVP@kohnert-n4>
Permalink:  ../1638919.PXEyjZ9AVP@kohnert-n4/
Also on:    postgresql.org/message-id/1638919.PXEyjZ9AVP@kohnert-n4

 · 

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: nospam001-lists@jan-kohnert.de
  Subject: Re: A GROUP BY question
  In-Reply-To: <1638919.PXEyjZ9AVP@kohnert-n4>
  References: <20190813095403.GA2013@panix.com> <20190813111018.GA19289@panix.com> <1638919.PXEyjZ9AVP@kohnert-n4>

* 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