X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 5D8D39FAD5A for ; Tue, 25 Jul 2006 23:34:49 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 31091-10 for ; Tue, 25 Jul 2006 23:34:43 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id D2A689FA68D for ; Tue, 25 Jul 2006 23:34:43 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from canyonero.dot.net.au (canyonero.dot.net.au [202.147.68.14]) by svr4.postgresql.org (Postfix) with ESMTP id 87D575AF035 for ; Wed, 26 Jul 2006 02:34:41 +0000 (GMT) Received: from [203.129.54.231] (helo=[203.129.54.231]) by canyonero.dot.net.au with esmtp (Exim 3.35 #1 (Debian)) id 1G5ZEG-0006NH-00 for ; Wed, 26 Jul 2006 12:34:32 +1000 Message-ID: <44C6D4B7.70003@homemail.com.au> Date: Wed, 26 Jul 2006 12:34:31 +1000 From: Ross Johnson User-Agent: Mozilla Thunderbird 1.0.7-1.1.fc3 (X11/20050929) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: About Div References: <20060726014328.38920.qmail@web53202.mail.yahoo.com> In-Reply-To: <20060726014328.38920.qmail@web53202.mail.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200607/313 X-Sequence-Number: 25887 Otniel Michael wrote: > Mr. Aaron. I am sorry, your solution didn't match in my case. > Example for your solution : > A = 1 > B = 1 > C = 1 > D = 1 > E = 1 > F = 1 > G = 4 > > G have 4 candy. Its too much for G. > > In my case, the solution is : > A = 1 > B = 1 > C = 1 > D = 1 > E = 2 > F = 2 > G = 2 > > The extra candy is given to three child. > > Do you have the other solution? I need function in postgresql for my case. > Because my loop is too slow. Would this achieve the result you're looking for, or must it be done with a function? Assuming you already know the value of totalchildren and totalcandy: BEGIN -- Everyone gets at least this number, which could be zero or more. UPDATE X SET value = (totalcandy / totalchildren); -- Hand the remainder out one at a time until all are gone. UPDATE X SET value = (value + 1) WHERE code = (SELECT code FROM X ORDER BY code DESC LIMIT (totalcandy % totalchildren)); COMMIT Ross