pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
using a function in where
3+ messages / 3 participants
[nested] [flat]

* using a function in where
@ 2018-12-03 07:06 Glenn Schultz <glenn@bondlab.io>
  2018-12-03 07:32 ` Re: using a function in where Andrew Gierth <andrew@tao11.riddles.org.uk>
  2018-12-03 08:10 ` Re: using a function in where Alban Hertroys <haramrae@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Glenn Schultz @ 2018-12-03 07:06 UTC (permalink / raw)
  To: pgsql-general

All,
I am using the function below to convert a continuous variable to a binned
value.  Sometimes a value other than zero is passed through the query. For
example -.5 result value is passed to the query result.  The basic of the
query is below.

select
incentivebin(wac, rate, .25)
from
my_table
where incentivebin(was, rate, .25) = 0

I have checked the function works correctly and the raw values match those
values expected from simple subtraction and are in the correct bin.  I am
not sure why some values would be allowed through the query. Any ideas
would be appreciated.

Best,
Glenn


CREATE or REPLACE FUNCTION embs_owner.IncentiveBin(IN "Gwac" double
precision,
  IN "MtgRate" double precision,
  IN "BinSize" double precision)
    RETURNS double precision
    LANGUAGE 'sql'
    PARALLEL SAFE
AS 'select ceiling(($1 - $2)/$3) *$3';

ALTER FUNCTION embs_owner.IncentiveBin(double precision, double precision,
double precision)
    OWNER TO embs_owner;

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: using a function in where
  2018-12-03 07:06 using a function in where Glenn Schultz <glenn@bondlab.io>
@ 2018-12-03 07:32 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
  1 sibling, 0 replies; 3+ messages in thread

From: Andrew Gierth @ 2018-12-03 07:32 UTC (permalink / raw)
  To: Glenn Schultz <glenn@bondlab.io>; +Cc: pgsql-general

>>>>> "Glenn" == Glenn Schultz <glenn@bondlab.io> writes:

 Glenn> All,
 Glenn> I am using the function below to convert a continuous variable
 Glenn> to a binned value. Sometimes a value other than zero is passed
 Glenn> through the query. For example -.5 result value is passed to the
 Glenn> query result. The basic of the query is below.

 Glenn> select
 Glenn> incentivebin(wac, rate, .25)
 Glenn> from
 Glenn> my_table
 Glenn> where incentivebin(was, rate, .25) = 0

Those two calls to incentivebin() have different parameters - is that an
error in the original query, or a typo in transcribing it to email?

-- 
Andrew (irc:RhodiumToad)




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: using a function in where
  2018-12-03 07:06 using a function in where Glenn Schultz <glenn@bondlab.io>
@ 2018-12-03 08:10 ` Alban Hertroys <haramrae@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Alban Hertroys @ 2018-12-03 08:10 UTC (permalink / raw)
  To: Glenn Schultz <glenn@bondlab.io>; +Cc: pgsql-general


> On 3 Dec 2018, at 8:06, Glenn Schultz <glenn@bondlab.io> wrote:
> 
> All,
> I am using the function below to convert a continuous variable to a binned value.  Sometimes a value other than zero is passed through the query. For example -.5 result value is passed to the query result.  The basic of the query is below.  
> 
> select
> incentivebin(wac, rate, .25)
> from
> my_table
> where incentivebin(was, rate, .25) = 0

> CREATE or REPLACE FUNCTION embs_owner.IncentiveBin(IN "Gwac" double precision, 

> AS 'select ceiling(($1 - $2)/$3) *$3';

If the difference of (wac - rate) = -0.5, and you multiply that by 4 (divide by .25), ceil will round that correctly to -2. If you then divide by 4 again, you get -0.5 back (which is twice your bin size).

The result matches your function definition with the given parameters.


Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2018-12-03 08:10 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-03 07:06 using a function in where Glenn Schultz <glenn@bondlab.io>
2018-12-03 07:32 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-12-03 08:10 ` Alban Hertroys <haramrae@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox