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 1hxWOi-0004WF-N4 for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 12:59:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hxWOh-0003ko-HZ for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 12:59:31 +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 1hxWOh-0003kP-5j for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 12:59:31 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxWOe-00077v-FA for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 12:59:30 +0000 Received: by mail-ed1-x52b.google.com with SMTP id h8so18696670edv.7 for ; Tue, 13 Aug 2019 05:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=dDQjhcvfTQIvS/lL3noioIH+bkCyz3183wUQvpf5dbQ=; b=IbObULn4cvtYWT/ckXm6xFNIsvzxepSKk7idh45/AWtzKJZkkGm9Y4gN5xxTIAduOM gmXjgnJGFLxD/NvjsZK2hyVSeWPcRk9gvDTJB09AQoXuR2hT+7sqxDuz+jXsKbqXWlJw pTgKoZs1w7FsDEbLLy5U8Ocu5dAVio65ZypjQoqsWowxsJkjGgT1NRKkHYLrx+O1JZdR oZC3XLpcszOOfli4RoZLWJ3iVkBHcC54AggAdw307NHtcNPNLdX/ngxumMR4IoVQ93IQ jf05ZCXPG7qAluUbakLCchI0E83iZgvRWdp7NcFZetRymagywXvoa8GvpOGO0ak36gta Ilew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=dDQjhcvfTQIvS/lL3noioIH+bkCyz3183wUQvpf5dbQ=; b=DD+n/P5QqQODzQJOGp9dOnrrc4aSI3+84B4YYdrM0j2j66aS9+tDTbz4I7mn9Wsc43 bDeGPfE2Y5u8sjjH+sTlPymzjRFGVwLCKmTD/7wVALRCjARMwEBM7m9/BtICkyPowqhh OQOjO/UUuMtzAo/ZosKYlRMVXDPKLWJa2Hdd3/SSDaMJGhPsd2tIgBPv7RwtrROKDw4E R1u59HYhOM+vOLyyOotGKT91yc7cu8vOa5v827EgbRuFHRbmn2loDdQwmAY1UfiQttIM y1L8+qGPXZCR0NXfHgkWC0RmOdHMelYJJm6uW1wqEuPcZbgvFgLF0sAMDQRn9NCVOOx8 uuuQ== X-Gm-Message-State: APjAAAXFMI7sdK2gSm7URt6VjMJWeTBq1I4AFcvIqQtbn7o1xUGyi99Y po6FbH5KwjiblUv2h6CbtYg= X-Google-Smtp-Source: APXvYqzaGYfBPI5InkJ5zHc6lq6y976t1+MRMYxGeJUv6eMy+5HTxh5PVGRMbi5WHoUiDCPBXG6fcQ== X-Received: by 2002:a50:8687:: with SMTP id r7mr23500647eda.137.1565701165255; Tue, 13 Aug 2019 05:59:25 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id u26sm25009071eda.22.2019.08.13.05.59.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Aug 2019 05:59:24 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: A GROUP BY question From: Alban Hertroys In-Reply-To: <20190813111018.GA19289@panix.com> Date: Tue, 13 Aug 2019 14:59:22 +0200 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <20190813095403.GA2013@panix.com> <20190813111018.GA19289@panix.com> To: stan X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 13 Aug 2019, at 13:10, stan wrote: >=20 > select=20 > project.proj_no , Removed columns that get in the way of your desired result. You can=E2=80=99= t have both details and the sum over them in a meaningful way. > SUM (rate.rate * task_instance.hours)=20 > from=20 > task_instance > join rate on=20 > rate.employee_key =3D task_instance.employee_key > AND > rate.work_type_key =3D task_instance.work_type_key (break) > inner join employee on > rate.employee_key =3D employee.employee_key > inner join work_type on > rate.work_type_key =3D work_type.work_type_key These are now probably redundant, you don=E2=80=99t need them unless = they filter your results. > inner join project on > project.project_key =3D task_instance.project_key And this JOIN could be dropped if project_key and proj_no weren=E2=80=99t = 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 = =E2=80=9Cwar=E2=80=9D between surrogate and natural keys I=E2=80=99m on = the natural keys side. Clearly, not everyone agrees on that. > GROUP BY=20 > project.project_key , Same columns removed here too. > ORDER BY=20 > project.proj_no=20 > ; 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. =46rom 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.