pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ross Johnson <Ross.Johnson@homemail.com.au>
To: pgsql-sql@postgresql.org
Subject: Re: About Div
Date: Wed, 26 Jul 2006 12:34:31 +1000
Message-ID: <44C6D4B7.70003@homemail.com.au> (raw)
In-Reply-To: <20060726014328.38920.qmail@web53202.mail.yahoo.com>
References: <20060726014328.38920.qmail@web53202.mail.yahoo.com>

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




view thread (5+ messages)  latest in thread

Message-ID: <44C6D4B7.70003@homemail.com.au>
Permalink:  ../44C6D4B7.70003@homemail.com.au/
Also on:    postgresql.org/message-id/44C6D4B7.70003@homemail.com.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: Ross.Johnson@homemail.com.au
  Subject: Re: About Div
  In-Reply-To: <44C6D4B7.70003@homemail.com.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox