Re: WITH clause

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: WITH clause
Date: 2003-12-14 02:56:00
Message-ID: 87iskkw1j3.fsf@stark.dyndns.tv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Hannu Krosing <hannu(at)tm(dot)ee> writes:

> SELECT x.*
> FROM x,
> (select match (x.foo, '([0-9]+)x([0-9]+)')
> from x innerx
> where innerx.pk = x.pk
> ) as res
> HAVING y = get_match_group(res, 2)
> OR y = get_match_group(res, 3)
> ;

Well you don't need to go fetch from the table an extra time. Presumably the
data will be cached but it's still a lot of extra work to process the data
twice.

You could just do

select *
from (
select x.*,
(select match(foo, '([0-9]+)x([0-9]+)') as res
)
where y = res[2]
or y = res[3]

But what Hannu's saying is that the SQL Standard WITH is precisely syntactic
sugar for subqueries used like above.

It sounds like WITH is to subqueries as let is to lambda....

--
greg

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Stark 2003-12-14 03:12:32 Re: ORDER BY and DISTINCT ON
Previous Message Bruno Wolff III 2003-12-14 00:22:36 Re: ORDER BY and DISTINCT ON