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 B200B9FA65C for ; Fri, 7 Jul 2006 05:21:02 -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 28975-05 for ; Fri, 7 Jul 2006 05:20:59 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.168]) by postgresql.org (Postfix) with ESMTP id 436979FA634 for ; Fri, 7 Jul 2006 05:20:58 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id a2so2997961ugf for ; Fri, 07 Jul 2006 01:20:57 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Xp9/c7iY8cFf+70l3pqI0W7L8HLpERZWUGAirjf8DeFQmavu0gC5TUtcTPU6qFlgUn0hvFcMD2g8NBA821Tv34Ny5eUQgwji50QLOahdQCeIfzoSnFBpqD8VTNYyGhMXMSWREeTlih7CZFZpJJM3UKCTX89WDWQ6Jvg/wJL3w9g= Received: by 10.67.29.12 with SMTP id g12mr1696693ugj; Fri, 07 Jul 2006 01:20:57 -0700 (PDT) Received: by 10.67.24.20 with HTTP; Fri, 7 Jul 2006 01:20:57 -0700 (PDT) Message-ID: Date: Fri, 7 Jul 2006 03:20:57 -0500 From: "Rodrigo De Leon" Reply-To: rdeleonp@gmail.com, rdeleonp@yahoo.com To: "Phillip Smith" Subject: Re: Select Maths Cc: pgsql-sql@postgresql.org In-Reply-To: <012b01c6a192$1b827730$9b0014ac@ITPhil> MIME-Version: 1.0 Content-Type: text/plain; charset=WINDOWS-1252; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <012b01c6a192$1b827730$9b0014ac@ITPhil> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/82 X-Sequence-Number: 25656 On 7/7/06, Phillip Smith wrote: > Hi again, G'day (it's 03:21 on a friday here). > Same SELECT query as before, different area of it=85 I have a function th= at > calculates the recommended purchase order quantity for a stock item based > off various other values and functions: > > pqty(stock.code) AS "pqty" > > This needs to be rounded up / down to the nearest multiple of the purchas= e > unit quantity for that product =96 It's Friday afternoon and my head has > refused to help me work out the maths all afternoon! > > Example: > > Pqty =3D 60 > > Purchase Unit =3D 25 > > Pqty needs to be rounded down to 50. create or replace function roundupdown(pqty int, punit int) returns int as $$ select ((case when $1<$2 then $2 else $1 end)/$2::float)::int*$2; $$ language 'sql'; -- test select roundupdown(s.x,25) as pqty , (s.x/25::float) as near from generate_series(1,100) s(x); > I guess I'm also asking if I should do this in the Pqty function or in th= e > SELECT query to optimize the result? Whatever suits your usage pattern, I guess. > Thanks all =96 Enjoy your weekends I hope! > > Cheers, > > -p Same to you. Regards, Rodrigo