Re: WITH clause - Mailing list pgsql-hackers

From Greg Stark
Subject Re: WITH clause
Date
Msg-id 87iskkw1j3.fsf@stark.dyndns.tv
Whole thread Raw
In response to Re: WITH clause  (Hannu Krosing <hannu@tm.ee>)
List pgsql-hackers
Hannu Krosing <hannu@tm.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



pgsql-hackers by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: ORDER BY and DISTINCT ON
Next
From: Greg Stark
Date:
Subject: Re: ORDER BY and DISTINCT ON