pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Richard Broersma <richard.broersma@gmail.com>
Cc: sql pgsql <pgsql-sql@postgresql.org>
Subject: Re: Learning about WITH RECURSIVE
Date: Wed, 04 Nov 2009 19:04:01 -0500
Message-ID: <144.1257379441@sss.pgh.pa.us> (raw)
In-Reply-To: <396486430911041427q3a0c9518v9c999a34290798ed@mail.gmail.com>
References: <396486430911041403u639c750bn488ada60822448c0@mail.gmail.com>
	<27333.1257372664@sss.pgh.pa.us>
	<396486430911041427q3a0c9518v9c999a34290798ed@mail.gmail.com>

Richard Broersma <richard.broersma@gmail.com> writes:
> Actually I'm still confused.  I must me missing something.  When I
> manually following the directions of:
> http://www.postgresql.org/docs/8.4/interactive/queries-with.html

> I get the following when I try:

> WITH RECURSIVE t(n) AS (
>     VALUES (1)
>   UNION ALL
>     SELECT n+1 FROM t WHERE n < 100
> )
> SELECT sum(n) FROM t;

> (1) --initial non-recursive working table

> (1) UA (2) = (1,2) --new(1) working table

> (1,2) UA (2,3) = (1,2,2,3) --new(2) working table

> (1,2,2,3) UA (2,3,3,4) = (1,2,2,2,3,3,3,4) --new(3) working table

You're confusing the working table with the final output.  In this
test case, the working table contains exactly one row after each
step (except after the last, when it contains no rows).  That one
row is also added to the result, but we don't use the whole result
for the next iteration of the recursive term.

			regards, tom lane



view thread (5+ messages)

Message-ID: <144.1257379441@sss.pgh.pa.us>
Permalink:  ../144.1257379441@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/144.1257379441@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, richard.broersma@gmail.com
  Subject: Re: Learning about WITH RECURSIVE
  In-Reply-To: <144.1257379441@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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