Обсуждение: Select Maths

Поиск
Список
Период
Сортировка

Select Maths

От
"Phillip Smith"
Дата:

Hi again,

 

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?

 

Thanks all – Enjoy your weekends I hope!

Cheers,

-p


*******************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

От
"Rodrigo De Leon"
Дата:
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

От
"Aaron Bono"
Дата:
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

От
"Phillip Smith"
Дата:

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

От
"Aaron Bono"
Дата:
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

От
"Phillip Smith"
Дата:

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

От
"Aaron Bono"
Дата:
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

От
"Phillip Smith"
Дата:

It ties back to my other post about the “FLAGS” column – I need to be able to find out if the original pqty() calculation has needed to be modified.

 

I guess what you’re saying is to have 2 functions – one that calculates the figure I have at the moment, then a second to return the adjusted figure?

 

Then I can use the first function when I’m working out what the flags need to be, then the second to give the actual adjusted figure….

 

-p

 

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


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


*******************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