Re: using a function in where

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Glenn Schultz <glenn(at)bondlab(dot)io>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: using a function in where
Date: 2018-12-03 08:10:28
Message-ID: 458B6999-8E88-4C9F-A999-8DA2659F0A6C@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> On 3 Dec 2018, at 8:06, Glenn Schultz <glenn(at)bondlab(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Igor Korot 2018-12-03 13:29:01 Re: How to watch for schema changes
Previous Message Andrew Gierth 2018-12-03 07:32:02 Re: using a function in where