Re: Let-bindings in SQL statements

From: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
To: Jon Smark <jon(dot)smark(at)yahoo(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Let-bindings in SQL statements
Date: 2012-01-26 19:18:10
Message-ID: CAP_rwwk3VAU7YLAy+vO1OY0R4f0JhkES-ojLufvWsRvqMs3X1w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jan 26, 2012 at 3:37 PM, Jon Smark <jon(dot)smark(at)yahoo(dot)com> wrote:

> Hi,
>
> Is it possible to do the equivalent of let-bindings in a pure SQL function?
> I have a SELECT that invokes "now" multiple times. It would be nicer
> to do it only once and reuse the value. Something like this:
>
> LET right_now = SELECT now () IN
> SELECT * FROM my_table WHERE right_now >= start AND ...
>
> In PL/pgSQL this is easy, but I wonder about SQL...
>
>
WITH param AS ( select now() as p_start, somefunc() as p_something )
SELECT * FROM param,my_table WHERE right_now >= param.p_start AND ...

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jason Long 2012-01-26 19:24:57 Optimizing Queries Joining Several Views
Previous Message panam 2012-01-26 17:56:33 Re: Best way to create unique primary keys across schemas?