Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 3CEF42E0042 for ; Mon, 19 Nov 2007 19:17:40 -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 91001-05 for ; Mon, 19 Nov 2007 19:17:35 -0400 (AST) Received: from andromeda1.neonex.com.br (dns3.neonex.com.br [201.28.22.10]) by postgresql.org (Postfix) with ESMTP id 341D42E003F for ; Mon, 19 Nov 2007 19:17:35 -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 1IuFru-000314-Ij; Mon, 19 Nov 2007 21:17:31 -0200 Message-ID: <47421986.1020509@planit.com.br> Date: Mon, 19 Nov 2007 21:17:26 -0200 From: "Luiz K. Matsumura" Organization: Plan IT Tecnologia =?UTF-8?B?SW5mb3Jtw6F0aWNhIEx0ZGEu?= User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: =?UTF-8?B?Um9kcmlnbyBEZSBMZcOzbg==?= CC: pgsql-sql@postgresql.org References: <4741F63A.6070500@planit.com.br> In-Reply-To: Content-Type: text/plain; charset=UTF-8; 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: rdeleonp@gmail.com, pgsql-sql@postgresql.org Subject: Re: 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/94 X-Sequence-Number: 29622 Hi Rodrigo, thanks for reply Rodrigo De León wrote: > On Nov 19, 2007 3:46 PM, Luiz K. Matsumura wrote: > >> If someone can give me some hint I will apreciate. >> > > This is more of a normalization problem. See: > > http://www.deeptraining.com/litwin/dbdesign/FundamentalsOfRelationalDatabaseDesign.aspx > > and lookup Third Normal Form. > > You have a calculated total for each order, so I assume there's a > price for every product. You should join order_item to whichever table > has the individual cost for each product and multiply it by > order_item.qty. > > Ok, the example is a simplified scenario. In some cases we have discount on order that can't be 'distributed' on the itens. For example: Item Qty ValueUnity item_total (calculated) 1 5 1.01 5.05 2 6 1.01 6.06 The total for itens is 11.11 but we make a discount of 0.01 , then the value of order is now 11.10 This 0.01 of discount can't be aplied on any "ValueUnity" because the precision is 2 decimals. We can have too other calculations over this values (like taxes) so in my case isn't so simple to determine the final value of the order just summing the value of the itens. Anyway, thanks again... -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda.