Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxWX7-0004tk-OM for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 13:08:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hxWX4-0002ye-TJ for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 13:08:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxWX4-0002yX-HL for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 13:08:10 +0000 Received: from jan-kohnert.de ([62.141.42.79]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxWX1-0007Cv-2K for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 13:08:10 +0000 X-Virus-Scanned: amavisd-new at jan-kohnert.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jan-kohnert.de; s=20191; t=1565701685; bh=c52jiouowiMqgmm9xupVGOXJ/7SDbKLuE6Sji584MiM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fXQ4NX1/mCJMPvPs+ppg3QVIcIsZP/+4urloo8Vd8ZGHvScxkTbPQG6ojYSz6ovV2 5z6fhAyfw/IFqh+tiqyVzJbhM/AHtO5ez0BC+ispq5VNnTiLJ2iYSag3VbSS+kSlMw f6BBWps3sn83N+J75Cw1rdrzyQcqaN2C0KRaxlVt7ECxxxin1AzsCYGkmGZTj09B4y 6wSYzcnFJT3hUCPS6Ocw+phdry2FPoiP+frZtwR3oS5EUWPypaAIZKgZJx3fdgdUSv ngzuDk0GWV3CzILlulTZg+gaGvxnYKt/hMkdKPxLBVCczY6xsngARJ91yWOxqeBy55 68nDC3MENSy3Q== Received: from kohnert-n4.localnet (unknown [IPv6:2a02:810a:143f:b6b8:d3b8:5b6c:5cf9:305b]) by b079.blue.fastwebserver.de (Postfix) with ESMTPSA id B84EB400AA969 for ; Tue, 13 Aug 2019 15:08:04 +0200 (CEST) From: Jan Kohnert 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> In-Reply-To: <20190813111018.GA19289@panix.com> References: <20190813095403.GA2013@panix.com> <20190813111018.GA19289@panix.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="nextPart5653508.SDCAvbsKtQ" Content-Transfer-Encoding: 7Bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --nextPart5653508.SDCAvbsKtQ Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" 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 --nextPart5653508.SDCAvbsKtQ Content-Transfer-Encoding: 7Bit Content-Type: text/html; charset="us-ascii"

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

--nextPart5653508.SDCAvbsKtQ--