Thread: Select Maths

Select Maths

From
"Phillip Smith"
Date:
<div class="Section1"><p class="MsoNormal"><font color="navy" face="Times New Roman" size="2"><span
style="font-size:11.0pt;color:navy">Hiagain,</span></font><p class="MsoNormal"><font color="navy" face="Times New
Roman"size="2"><span style="font-size:11.0pt;color:navy"> </span></font><p class="MsoNormal"><font color="navy"
face="TimesNew Roman" size="2"><span style="font-size:11.0pt;color:navy">Same SELECT query as before, different area of
it…I have a function that calculates the recommended purchase order quantity for a stock item based off various other
valuesand functions:</span></font><p class="MsoNormal" style="text-indent:36.0pt"><font color="navy" face="Courier New"
size="2"><spanstyle="font-size:11.0pt;font-family:"Courier New"; 
color:navy">pqty(stock.code) AS "pqty"</span></font><p class="MsoNormal"><font color="navy" face="Times New Roman"
size="2"><spanstyle="font-size:11.0pt;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Times
NewRoman" size="2"><span style="font-size:11.0pt;color:navy">This needs to be rounded up / down to the nearest multiple
ofthe <u>purchase unit</u> quantity for that product – It’s Friday afternoon and my head has refused to help me work
outthe maths all afternoon!</span></font><p class="MsoNormal"><font color="navy" face="Times New Roman" size="2"><span
style="font-size:11.0pt;color:navy"> </span></font><pclass="MsoNormal"><font color="navy" face="Times New Roman"
size="2"><spanstyle="font-size:11.0pt;color:navy">Example:</span></font><p class="MsoNormal"><font color="navy"
face="CourierNew" size="2"><span style="font-size:11.0pt;font-family:"Courier New";color:navy">     Pqty =
60</span></font><pclass="MsoNormal"><font color="navy" face="Courier New" size="2"><span
style="font-size:11.0pt;font-family:"CourierNew";color:navy">     Purchase Unit = 25</span></font><p
class="MsoNormal"><fontcolor="navy" face="Times New Roman" size="2"><span style="font-size:11.0pt;color:navy">Pqty
needsto be rounded down to 50.</span></font><p class="MsoNormal"><font color="navy" face="Times New Roman"
size="2"><spanstyle="font-size:11.0pt;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Times
NewRoman" size="2"><span style="font-size:11.0pt;color:navy">I guess I’m also asking if I should do this in the Pqty
functionor in the SELECT query to optimize the result?</span></font><p class="MsoNormal"><font color="navy" face="Times
NewRoman" size="2"><span style="font-size:11.0pt;color:navy"> </span></font><p class="MsoNormal"><font color="navy"
face="TimesNew Roman" size="2"><span style="font-size:11.0pt;color:navy">Thanks all – Enjoy your weekends I
hope!</span></font><pclass="MsoNormal"><font color="navy" face="Times New Roman" size="2"><span
style="font-size:11.0pt;color:navy">Cheers,</span></font><pclass="MsoNormal"><font color="navy" face="Times New Roman"
size="2"><spanstyle="font-size:11.0pt;color:navy">-p</span></font></div><br /><p><b>*******************Confidentiality
andPrivilege Notice*******************</b><p> The material contained in this message is privileged and confidential to
theaddressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such
person,you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by
replyemail. <p> Information in this message that does not relate to the official business of Weatherbeeta must be
treatedas neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not
beliable for direct, indirect or consequential loss arising from transmission of this message or any attachments <br /> 

Re: Select Maths

From
"Rodrigo De Leon"
Date:
On 7/7/06, Phillip Smith <phillips@weatherbeeta.com.au> wrote:
> Hi again,

G'day (it's 03:21 on a friday here).

> Same SELECT query as before, different area of it… I have a function that
> 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 purchase
> unit quantity for that product – It's Friday afternoon and my head has
> refused to help me work out the maths all afternoon!
>
> Example:
>
>      Pqty = 60
>
>      Purchase Unit = 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 the
> SELECT query to optimize the result?

Whatever suits your usage pattern, I guess.

> Thanks all – Enjoy your weekends I hope!
>
> Cheers,
>
> -p

Same to you.

Regards,

Rodrigo


Re: Select Maths

From
"Aaron Bono"
Date:
On 7/7/06, Phillip Smith <phillips@weatherbeeta.com.au> wrote:

Same SELECT query as before, different area of it… I have a function that 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 purchase unit quantity for that product – It's Friday afternoon and my head has refused to help me work out the maths all afternoon!

 

Example:

     Pqty = 60

     Purchase Unit = 25

Pqty needs to be rounded down to 50.

 

I guess I'm also asking if I should do this in the Pqty function or in the SELECT query to optimize the result?


select 25 * round(cast(60 as double precision) / 25)

Re: Select Maths

From
"Phillip Smith"
Date:

Beautiful – Works a treat. Thanks Aaron.

 

A follow-on problem now… I have the below column in the select, but I need to validate the value across all 3 rules –  I need to assign it to a variable!!

 

Example – my pqty function calculates a value less than the suppliers minimum order qty (and therefore fails the first CASE below), I need to set the column to a new value (stock.purchase_unit) – That’s all OK. But I need to check this new value against the remaining 2 CASE’s…

 

<SNIP>

CASE  WHEN pqty(stock.code) < stock.purchase_unit THEN stock.purchase_unit

            --^^^-- Check that our suggested purchase qty is greater than then suppliers minimum order qty

      WHEN MOD(pqty(stock.code), stock.box_qty) > 0 THEN stock.box_qty * ROUND(CAST(pqty(stock.code) AS DOUBLE PRECISION) / stock.box_qty)

            --^^^-- Check that our suggested purchase qty is a multiple of the box qty

      WHEN pqty(stock.code) < (urate(stock.code) * creditors.review_cycle) THEN urate(stock.code) * creditors.review_cycle

            --^^^-- Check that our suggested purchase qty is greater than our Usage Rate x Creditor Review Cycle

END AS "pqty",

<SNIP>

 

Thanks again for all your help guys,

-p

 

-----Original Message-----
From: aaron.bono@gmail.com [mailto:aaron.bono@gmail.com] On Behalf Of Aaron Bono
Sent:
Friday, 7 July 2006 18:37
To: Phillip Smith
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Select Maths

 

On 7/7/06, Phillip Smith <phillips@weatherbeeta.com.au> wrote:

Same SELECT query as before, different area of it… I have a function that 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 purchase unit quantity for that product – It's Friday afternoon and my head has refused to help me work out the maths all afternoon!

 

Example:

     Pqty = 60

     Purchase Unit = 25

Pqty needs to be rounded down to 50.

 

I guess I'm also asking if I should do this in the Pqty function or in the SELECT query to optimize the result?


select 25 * round(cast(60 as double precision) / 25)

 


*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.

Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments

Re: Select Maths

From
"Aaron Bono"
Date:
On 7/10/06, Phillip Smith <phillips@weatherbeeta.com.au> wrote:

Beautiful – Works a treat. Thanks Aaron.

 

A follow-on problem now… I have the below column in the select, but I need to validate the value across all 3 rules –  I need to assign it to a variable!!

 

Example – my pqty function calculates a value less than the suppliers minimum order qty (and therefore fails the first CASE below), I need to set the column to a new value (stock.purchase_unit) – That's all OK. But I need to check this new value against the remaining 2 CASE's…

 

<SNIP>

CASE  WHEN pqty(stock.code) < stock.purchase_unit THEN stock.purchase_unit

            --^^^-- Check that our suggested purchase qty is greater than then suppliers minimum order qty

      WHEN MOD(pqty(stock.code), stock.box_qty) > 0 THEN stock.box_qty * ROUND(CAST(pqty(stock.code) AS DOUBLE PRECISION) / stock.box_qty)

            --^^^-- Check that our suggested purchase qty is a multiple of the box qty

      WHEN pqty(stock.code) < (urate(stock.code) * creditors.review_cycle) THEN urate(stock.code) * creditors.review_cycle

            --^^^-- Check that our suggested purchase qty is greater than our Usage Rate x Creditor Review Cycle

END AS "pqty",

<SNIP>


Can you provide example values and show where it is and is not working?  I am not quite sure what you are trying to do here.

-Aaron 

Re: Select Maths

From
"Phillip Smith"
Date:

Example:

Funcation pqty(stock.code) calculates a value of 0 for a particular product. This fails the last CASE that makes sure the pqty() value is greater than our Usage Rate * Review Cycle – in this case is 3. But that is less than our Minimum Order Qty (First CASE) and not a multiple of our Box Qty (Second CASE)

Another example could be that pqty() calculates less than the Minimum Order Qty (fails first CASE) so we raise it to the Minimum Order Qty, but that new value could fail either or both of the second CASE’s.

Minimum Order Qty = stock.purchase_unit
Box Qty = stock.box_qty

I guess a better way to word it is that because pqty() returns a calculated value each time and I can’t take that value and assign it to a variable, then use that variable. If I was writing VB or similar I’d want something like:

intPurchaseQty = pqty(stock.code)

CASE  WHEN intPurchaseQty < stock.purchase_unit THEN intPurchaseQty = stock.purchase_unit

WHEN MOD(intPurchaseQty, stock.box_qty) > 0 THEN intPurchaseQty = stock.box_qty * ROUND(CAST(intPurchaseQty AS DOUBLE PRECISION) / stock.box_qty)

WHEN intPurchaseQty < (urate(stock.code) * creditors.review_cycle) THEN intPurchaseQty = urate(stock.code) * creditors.review_cycle

END

COLUMN = intPurchaseQty AS "pqty",

I hope that makes it a lighter shade of mud!!

 

-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Aaron Bono
Sent:
Tuesday, 11 July 2006 02:36
To: Phillip Smith
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Select Maths


Can you provide example values and show where it is and is not working?  I am not quite sure what you are trying to do here.

-Aaron 

 


*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.

Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments

Re: Select Maths

From
"Aaron Bono"
Date:
On 7/10/06, Phillip Smith <phillips@weatherbeeta.com.au> wrote:

Example:

Funcation pqty(stock.code) calculates a value of 0 for a particular product. This fails the last CASE that makes sure the pqty() value is greater than our Usage Rate * Review Cycle – in this case is 3. But that is less than our Minimum Order Qty (First CASE) and not a multiple of our Box Qty (Second CASE)

Another example could be that pqty() calculates less than the Minimum Order Qty (fails first CASE) so we raise it to the Minimum Order Qty, but that new value could fail either or both of the second CASE's.

Minimum Order Qty = stock.purchase_unit
Box Qty = stock.box_qty

I guess a better way to word it is that because pqty() returns a calculated value each time and I can't take that value and assign it to a variable, then use that variable. If I was writing VB or similar I'd want something like:

intPurchaseQty = pqty(stock.code)

CASE  WHEN intPurchaseQty < stock.purchase_unit THEN intPurchaseQty = stock.purchase_unit

WHEN MOD(intPurchaseQty, stock.box_qty) > 0 THEN intPurchaseQty = stock.box_qty * ROUND(CAST(intPurchaseQty AS DOUBLE PRECISION) / stock.box_qty)

WHEN intPurchaseQty < (urate(stock.code) * creditors.review_cycle) THEN intPurchaseQty = urate(stock.code) * creditors.review_cycle

END

COLUMN = intPurchaseQty AS "pqty",

I hope that makes it a lighter shade of mud!!


Why wouldn't you be able to do this in a function?  Pass in stock.code, stock.purchase_unit, stock.box_qty and creditors.review_cycle .  You can then use variables in the function, right?

-Aaron

Re: Select Maths

From
"Phillip Smith"
Date:
<div class="Section1"><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size:
10.0pt;font-family:Arial;color:navy">It ties back to my other post about the “FLAGS” column – I need to be able to find
outif the original pqty() calculation has needed to be modified.</span></font><p class="MsoNormal"><font color="navy"
face="Arial"size="2"><span style="font-size: 
10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span
style="font-size:
10.0pt;font-family:Arial;color:navy">I guess what you’re saying is to have 2 functions – one that calculates the figure
Ihave at the moment, then a second to return the adjusted figure?</span></font><p class="MsoNormal"><font color="navy"
face="Arial"size="2"><span style="font-size: 
10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span
style="font-size:
10.0pt;font-family:Arial;color:navy">Then I can use the first function when I’m working out what the flags need to be,
thenthe second to give the actual adjusted figure….</span></font><p class="MsoNormal"><font color="navy" face="Arial"
size="2"><spanstyle="font-size: 
10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span
style="font-size:
10.0pt;font-family:Arial;color:navy">-p</span></font><p class="MsoNormal"><font color="navy" face="Arial"
size="2"><spanstyle="font-size: 
10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal" style="margin-left:36.0pt"><font face="Tahoma"
size="2"><spanlang="EN-US" style="font-size:10.0pt;font-family:Tahoma">-----Original Message-----<br /><b><span
style="font-weight:bold">From:</span></b>aaron.bono@gmail.com [mailto:aaron.bono@gmail.com] <b><span
style="font-weight:bold">OnBehalf Of </span></b>Aaron Bono<br /><b><span style="font-weight:bold">Sent:</span></b>
</span></font><fontface="Tahoma" size="2"><span lang="EN-US" style="font-size:10.0pt;font-family:Tahoma">Tuesday, 11
July2006</span></font><font face="Tahoma" size="2"><span lang="EN-US" style="font-size:10.0pt;font-family:Tahoma">
</span></font><fontface="Tahoma" size="2"><span lang="EN-US"
style="font-size:10.0pt;font-family:Tahoma">13:42</span></font><fontface="Tahoma" size="2"><span lang="EN-US"
style="font-size:10.0pt;font-family:Tahoma"><br/><b><span style="font-weight:bold">To:</span></b> Phillip Smith<br
/><b><spanstyle="font-weight:bold">Cc:</span></b> pgsql-sql@postgresql.org<br /><b><span
style="font-weight:bold">Subject:</span></b>Re: [SQL] Select Maths</span></font><p class="MsoNormal"><font face="Times
NewRoman" size="3"><span style="font-size: 
12.0pt"><br /> Why wouldn't you be able to do this in a function?  Pass in stock.code, stock.purchase_unit,
stock.box_qtyand creditors.review_cycle .  You can then use variables in the function, right?<br /><br /> -Aaron
</span></font></div><br/><p><b>*******************Confidentiality and Privilege Notice*******************</b><p> The
materialcontained in this message is privileged and confidential to the addressee. If you are not the addressee
indicatedin this message or responsible for delivery of the message to such person, you may not copy or deliver this
messageto anyone, and you should destroy it and kindly notify the sender by reply email. <p> Information in this
messagethat does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by
Weatherbeeta.Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or
consequentialloss arising from transmission of this message or any attachments <br />