Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 4C72B2E003B for ; Mon, 19 Nov 2007 16:47:07 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 38532-09-2 for ; Mon, 19 Nov 2007 16:47:00 -0400 (AST) Received: from andromeda1.neonex.com.br (dns3.neonex.com.br [201.28.22.10]) by postgresql.org (Postfix) with ESMTP id 1AB1D2E0038 for ; Mon, 19 Nov 2007 16:47:00 -0400 (AST) Received: from [200.206.25.4] (helo=[127.0.0.1]) by andromeda1.neonex.com.br with esmtpa (Exim 4.63) (envelope-from ) id 1IuDWA-0002h0-A6 for pgsql-sql@postgresql.org; Mon, 19 Nov 2007 18:46:55 -0200 Message-ID: <4741F63A.6070500@planit.com.br> Date: Mon, 19 Nov 2007 18:46:50 -0200 From: "Luiz K. Matsumura" Organization: Plan IT Tecnologia =?ISO-8859-1?Q?Inform=E1tica_Ltda=2E?= User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Antivirus: avast! (VPS 071119-1, 11/19/2007), Outbound message X-Antivirus-Status: Clean X-SA-Exim-Connect-IP: 200.206.25.4 X-SA-Exim-Rcpt-To: pgsql-sql@postgresql.org Subject: Aggregate question (Sum) X-SA-Exim-Version: 4.2.1 (built Tue, 09 Jan 2007 17:23:22 +0000) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/92 X-Sequence-Number: 29620 Hi All, I want to know if there are an easy manner to do an SQL like this bellow where TotalOrdersValue sum the order.total just one time per order (as count(DISTINCT order.id) do) SELECT order.dtorder , Count( DISTINCT order.fk_customer ) AS QtyCustomer , Count( DISTINCT order.id ) AS QtyOrder , Sum( order_item.qty ) AS TotalQtyItem , Sum( order.total ) AS TotalOrders FROM order JOIN order_item ON order_item.fk_order = order.id GROUP BY 1 ORDER BY 1 Ex. ORDER Id | dtorder | fk_customer | total ----------------------------------------- 1 | 2007-01-01 | 1 | 100.00 2 | 2007-01-01 | 1 | 30.00 order_item fk_order | qty | fk_product -------------------------------- 1 | 5 | A 1 | 2 | B 2 | 3 | C The query acctualy returns (as expected): dtorder | QtyCustomer | QtyOrder | TotalQtyItem | TotalOrders ------------------------------------------------------------------ 2007-01-01 | 1 | 2 | 10 | 230.00 But I want dtorder | QtyCustomer | QtyOrder | TotalQtyItem | TotalOrders ------------------------------------------------------------------ 2007-01-01 | 1 | 2 | 10 | 130.00 I just want to avoid to do, if possible, things like: SELECT totals.dtorder , totals.QtyCustomer , totals.QtyOrder , totals.TotalQtyItem , Sum( order.total ) AS TotalOrders FROM ( SELECT order.dtorder , Count( DISTINCT order.fk_customer ) AS QtyCustomer , Count( DISTINCT order.id ) AS QtyOrder , Sum( order_item.qty ) AS TotalQtyItem FROM order JOIN order_item ON order_item.fk_order = order.id GROUP BY 1 ) totals JOIN order ON order.dtorder = totals.dtorder GROUP BY 1,2,3,4 ORDER BY totals.dtorder I say this because it's seem a waste of effort just to sum a value that can be calculated on the same loop where postgresql will go on table order... If someone can give me some hint I will apreciate. Tanks in advance. -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda.