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 1hxUh9-0000LY-FK for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 11:10:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hxUh8-0001gi-9v for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 11:10:26 +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 1hxUh7-0001g9-Vl for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 11:10:26 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxUh4-0006Bk-NO for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 11:10:25 +0000 Received: from panix2.panix.com (panix2.panix.com [166.84.1.2]) by mailbackend.panix.com (Postfix) with ESMTP id 46793t4SFLz1ShB for ; Tue, 13 Aug 2019 07:10:18 -0400 (EDT) Received: by panix2.panix.com (Postfix, from userid 17622) id 46793t3Drjz1ZVM; Tue, 13 Aug 2019 07:10:18 -0400 (EDT) Date: Tue, 13 Aug 2019 07:10:18 -0400 From: stan To: pgsql-general@lists.postgresql.org Subject: Re: A GROUP BY question Message-ID: <20190813111018.GA19289@panix.com> References: <20190813095403.GA2013@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190813095403.GA2013@panix.com> X-Editor: gVim User-Agent: Mutt/1.12.1 (2019-06-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Aug 13, 2019 at 05:54:03AM -0400, stan wrote: > I am trying to write, what is for me, a fairly complex query. It uses JOINS, > and also GROUP BY. I have this working with the exception of adding the > GROUP BY clause. > > Is there some reason I cannot add a GROUP BY function to a JOIN? > > Here is what I have: > > > 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 , > SUM(rate.rate * task_instance.hours) > ^^^^^^^^^^^^^^ > from > task_instance > GROUP BY > ^^^^^^^^^^^^^^^^^^ > project.project_key > ^^^^^^^^^^^^^^^^^^^^^^ > 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 > ORDER BY > project.proj_no , > employee.id > ; > 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. I did get the following to be accepted from a syntax basis, but it returns rows with the product for each row, and something in the sum column which is the same. DROP view tasks_view ; 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 , 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 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 ORDER BY project.proj_no ; -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin