Re: About Div - Mailing list pgsql-sql

From Aaron Bono
Subject Re: About Div
Date
Msg-id bf05e51c0607250800p44e98ce7uea04b19ff0cf8eee@mail.gmail.com
Whole thread Raw
In response to About Div  (Otniel Michael <otmic_ie@yahoo.com>)
Responses Re: About Div
List pgsql-sql
On 7/25/06, Otniel Michael <otmic_ie@yahoo.com> wrote:
Dear All,

I have a problem with this case :

I have 10 candy for 7 child (A, B, C, D, E, F, G).

Table X :
code   value
 -------  --------
 A        0       
 B        0       
 C        0       
 D        0       
 E        0       
 F        0       
 G       0       

And I want divide it with this algorithm :
    A = 10 / 7 = 1
    B = (10-1) / (7-1) = 9 / 6 = 1
    C = (10-2) / (7-2) = 8 / 5 = 1
    D = (10-3) / (7-3) = 7 / 4 = 1
    E = (10-4) / (7-4) = 6 / 3 = 2
    F = (10-6) / (7-5) = 4 / 2 = 2
    G = (10-8) / (7-6) = 2 / 2 = 2

In my first solution i use loop - for each record in my function.
But it is too slow in a lot of data.
Did postgresql have a function for my case?

 
No loop necessary.  This is a simple math problem:

dividedamount := candy / childcount;
extra = candy % childcount;

So the first (childcount - extra) get (dividedamount) pieces of candy and the last (extra) get (dividedamount + 1) pieces of candy.

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
==================================================================

pgsql-sql by date:

Previous
From: Otniel Michael
Date:
Subject: About Div
Next
From: "Aaron Bono"
Date:
Subject: Storage of Binary Data