Re: Novice PL/pgSQL question and example - Mailing list pgsql-novice

From Jasen Betts
Subject Re: Novice PL/pgSQL question and example
Date
Msg-id hl0ppo$mjg$1@reversiblemaps.ath.cx
Whole thread Raw
In response to Novice PL/pgSQL question and example  (James Long <pgsql-novice@museum.rain.com>)
List pgsql-novice
On 2010-02-08, James Long <pgsql-novice@museum.rain.com> wrote:

Newsgroups: gmane.comp.db.postgresql.novice
From: Jasen Betts <jasen@xnet.co.nz>
Subject: Re: Novice PL/pgSQL question and example
References: <20100208013329.GA95096@ns.umpquanet.com>
Organization: Dis (not Dat) Organisation
Followup-To:
X-Face:
?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s;a+M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W;(T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6{T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q;8"9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^

On 2010-02-08, James Long <pgsql-novice@museum.rain.com> wrote:
> At least, I hope this is a Novice question.  It sure makes me
> feel like one! :)  Please cc: me directly on replies, as I might
> miss a reply that goes only to the list.
>
> I'm running PostGreSQL 8.3.9 on FreeBSD 6.4-STABLE, but I doubt
> this is platform-dependent.
>
> I'm trying to solve what I'm sure is a common problem in the
> accounting world.  A total cost C has to be split across N
> different accounts, and C is not necessarily divisible by N.
> Shares with fractions of pennies are not allowable, and to make
> the accounting balance, the sum of all the shares has to sum to
> exactly match the total cost C.
>
> It's like splitting a $90 lunch check between 7 people.
>

> This PHP code shows the math I'm using, and works great.

here's different way to do it.

 share ( pieces , people, index ) = floor (pieces * (index + 1) / people ) - floor ( pieces * index / people)

you don't need to remember an error just increase the index each time.


pgsql-novice by date:

Previous
From: "Balma Robin Gordon"
Date:
Subject: Using xpath queries against XML Datatype
Next
From: Tim Landscheidt
Date:
Subject: Re: Using xpath queries against XML Datatype