pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedrecursive queries?
27+ messages / 19 participants
[nested] [flat]
* recursive queries?
@ 2000-04-09 16:25 Ron Peterson <rpeterson@yellowbank.com>
2000-04-10 13:48 ` Re: recursive queries? Andrew Schmeder <andy@qabobala.xapnet.com>
2000-04-10 20:45 ` Re: recursive queries? Peter Eisentraut <peter_e@gmx.net>
0 siblings, 2 replies; 27+ messages in thread
From: Ron Peterson @ 2000-04-09 16:25 UTC (permalink / raw)
To: pgsql-general
Now and again, I find myself wanting to store data in some kind of
variable-level hierarchy. To take a familiar example, let's say the
directory structure on my computer.
So I start to do something like:
CREATE SEQUENCE directory_id_seq;
CREATE TABLE directory {
parent INTEGER,
name TEXT,
id INTEGER
DEFAULT nextval('directory_id_seq')
PRIMARY KEY
};
INSERT INTO directory
VALUES (1, '\.');
ALTER TABLE directory
ADD FORIEGN KEY (parent)
REFERENCES directory(id)
ON DELETE CASCADE
ON UPDATE CASCADE;
Happy, happy. The problem is, while it's easy enough to define such a
data structure, support for recursive queries is rather lacking. To
unroll a directory tree, you basically have to resort to programming in
<insert your favorite language>.
Not that I really know what I'm talking about when I say 'unroll'. This
data structure is general enough to support cyclic directed graphs. So
what does it mean to 'unroll' such a beasty?
So what's my question then? Well, it seems like maybe there _should_,
or at least _could_ be support for some kinds of common problems. For
example, I would like to constrain my data structure to really be a
tree, and not a potentially cyclical graph.
Does anyone know whether there has been, or ever will be, movement in
this direction among the SQL literati? Am I asking a completely
inappropriate question?
Perhaps these types of problems are what OODB adherents are attempting
to address.
So, to sum, my question: Who plays in this space? Will the SQL
standard itself ever play in this space?
Personally, I'm really only interested in something elegant. Meaning I
don't want to mess around with a solution where this broker communicates
with that broker via an n-way blah blah blah. I can maintain literacy
in several tools at once, but not several dozen. Is my best bet simply
to accept SQL's limitations and program around them in C++ (or C, or
Python, or Perl, etc.)?
Ron Peterson
rpeterson@yellowbank.com
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: recursive queries?
2000-04-09 16:25 recursive queries? Ron Peterson <rpeterson@yellowbank.com>
@ 2000-04-10 13:48 ` Andrew Schmeder <andy@qabobala.xapnet.com>
1 sibling, 0 replies; 27+ messages in thread
From: Andrew Schmeder @ 2000-04-10 13:48 UTC (permalink / raw)
To: Ron Peterson <rpeterson@yellowbank.com>; +Cc: pgsql-general
> Personally, I'm really only interested in something elegant. Meaning I
> don't want to mess around with a solution where this broker communicates
> with that broker via an n-way blah blah blah. I can maintain literacy
> in several tools at once, but not several dozen. Is my best bet simply
> to accept SQL's limitations and program around them in C++ (or C, or
> Python, or Perl, etc.)?
Good question! I have certainly given this one some thought before. One of my
projects involved a directory structure of sorts which allowed non-cyclic
symlinking. My solution in this case was to create a seperate table that was a
'map' -- the map was a pre-calculated, flattened version of the entire graph.
Thus, a single query of the map table for "/blah/blah2/etc/" returned
immediately correct subdirectory. Each time the linking tables are updated,
the map table must be re-calculated. This solution will not work for the
general cyclic graph, of course... and it may be inappropriate for the
situation where the link tables are update frequently since each update forces
a large recursive caluculation of the map -- the primary advantage is the speed
and easy of querying.
I am not aware of any SQL constructs of 'for' loops or 'while' loops, so I am
not sure how one could solve the problem in pure SQL -- actually, you probably
could using a recursive stored procedure.
Andy
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: recursive queries?
2000-04-09 16:25 recursive queries? Ron Peterson <rpeterson@yellowbank.com>
@ 2000-04-10 20:45 ` Peter Eisentraut <peter_e@gmx.net>
2000-04-11 15:21 ` Re: recursive queries? Ron Peterson <rpeterson@yellowbank.com>
1 sibling, 1 reply; 27+ messages in thread
From: Peter Eisentraut @ 2000-04-10 20:45 UTC (permalink / raw)
To: Ron Peterson <rpeterson@yellowbank.com>; +Cc: pgsql-general
Ron Peterson writes:
> The problem is, while it's easy enough to define such a data
> structure, support for recursive queries is rather lacking. To unroll
> a directory tree, you basically have to resort to programming in
> <insert your favorite language>.
Well, nothing is perfect, and this is exactly the point where relational
databases aren't. A lot of people that are a lot more educated than me on
the matter have commented on just this in detail for years and decades so
you should be able to find enough material out there if you're interested.
Apparently, this sort of thing worked in network and/or hierarchical
databases (which came before relational) and object oriented databases are
supposes to fix it again, but the fact that all the world uses relational
databases shows that either recursion (infinite joins) isn't widely useful
or that the other advantages outweigh this problem. Not sure.
--
Peter Eisentraut Sernanders väg 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: recursive queries?
2000-04-09 16:25 recursive queries? Ron Peterson <rpeterson@yellowbank.com>
2000-04-10 20:45 ` Re: recursive queries? Peter Eisentraut <peter_e@gmx.net>
@ 2000-04-11 15:21 ` Ron Peterson <rpeterson@yellowbank.com>
0 siblings, 0 replies; 27+ messages in thread
From: Ron Peterson @ 2000-04-11 15:21 UTC (permalink / raw)
To: Peter Eisentraut <peter_e@gmx.net>; +Cc: pgsql-general
Peter Eisentraut wrote:
>
> Ron Peterson writes:
>
> > The problem is, while it's easy enough to define such a data
> > structure, support for recursive queries is rather lacking. To unroll
> > a directory tree, you basically have to resort to programming in
> > <insert your favorite language>.
>
> Well, nothing is perfect, and this is exactly the point where relational
> databases aren't. A lot of people that are a lot more educated than me on
> the matter have commented on just this in detail for years and decades so
> you should be able to find enough material out there if you're interested.
> Apparently, this sort of thing worked in network and/or hierarchical
> databases (which came before relational) and object oriented databases are
> supposes to fix it again, but the fact that all the world uses relational
> databases shows that either recursion (infinite joins) isn't widely useful
> or that the other advantages outweigh this problem. Not sure.
Don't get me wrong, I think SQL is wonderful. That's why this problem
is so fustrating. What I want to do seems _almost_ in reach, but not
quite. Defining recursive data structures is certainly possible in SQL,
but that's as far as it goes. There's no standard way to define
validation rules, or recursive queries.
I have read various mail threads and whatnot that have alluded to sQL
standards committees looking at the possibility of remedying these
deficiencies. But I haven't been able to find any evidence that these
efforts are actually underway. Does anyone know what direction the
evolution of the SQL standard is headed?
I keep struggling along with SQL not because I don't think recursive
data structures are useful, but as you say, "the other advantages
outweigh this problem". How would they be useful? It's not hard to
come up with some examples of useful problems where SQL falls short.
How about a genealogical database? Threaded mail or news discussions?
Web site layouts. Etc. I think all of these examples could benifit
from being implemented in a system that support transactions and
relational calculus.
I suppose I'm just another lazy activist - "Somebody should do
something!" I can suffer along for now, but what I'm really wondering
is if the investment I make now in SQL will be rewarded later when SQL
adds new and better features. Should I just hold my horses because
something better is coming along, or look elsewhere?
Ron Peterson
rpeterson@yellowbank.com
^ permalink raw reply [nested|flat] 27+ messages in thread
* Recursive queries
@ 2000-09-14 14:45 Rob Hoopman <uithuis@dds.nl>
0 siblings, 0 replies; 27+ messages in thread
From: Rob Hoopman @ 2000-09-14 14:45 UTC (permalink / raw)
To: pgsql-general
Hi all,
I have a question that probably is more apropriate for pgsql-dummies but
since I use postgres and don't know what list would be more
apropriate... here goes:
I have some data that I want to be able to search by geographic location.
The geographic locations are stored in a table with a reference to their
parent location (i.e. Germany has a reference to Europe whis is in the
same table)
I am wondering what the best aproach is when I want to perform a search
by location and need to include all child locations.
For example I want to return all results applying to the European Union
which means it should include all childcountries, provinces, cities, etc
in the search.
Any suggestions, or pointers to sources of info?
Thanks,
Rob Hoopman
^ permalink raw reply [nested|flat] 27+ messages in thread
* Recursive queries
@ 2001-09-21 16:14 Juan Jose Comellas <juanjo@comellas.org>
2001-09-21 18:46 ` Re: Recursive queries Jeff Eckermann <jeckermann@verio.net>
0 siblings, 1 reply; 27+ messages in thread
From: Juan Jose Comellas @ 2001-09-21 16:14 UTC (permalink / raw)
To: pgsql-general
I have a database with the following fields:
product_id INTEGER
parent_id INTEGER
name VARCHAR(64)
The idea is that it will store a tree of products and its subproducts. Any
row whose parent_id is NULL will be assumed to be a root product (i.e. top
level).
This means I will need to process recursively, with some sort of reference to
parent_id passing through layers of recursion.
However, I want to save on database queries and do a "SELECT *", then fetch
each row on the results recursively, to build the tree. The idea is to only
have to do one db query.
Does anyone have any idea how this can be done in PostgreSQL? Both Oracle and
DB2 support this with their own proprietary syntax.
--
Juan Jose Comellas
(juanjo@comellas.org)
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2001-09-21 16:14 Recursive queries Juan Jose Comellas <juanjo@comellas.org>
@ 2001-09-21 18:46 ` Jeff Eckermann <jeckermann@verio.net>
0 siblings, 0 replies; 27+ messages in thread
From: Jeff Eckermann @ 2001-09-21 18:46 UTC (permalink / raw)
To: Juan Jose Comellas <juanjo@comellas.org>; pgsql-general
There was a detailed post by Joe Celko not long ago, which discussed how to
handle trees in SQL. I suggest you look at the list archives to find this.
----- Original Message -----
From: "Juan Jose Comellas" <juanjo@comellas.org>
To: <pgsql-general@postgresql.org>
Sent: Friday, September 21, 2001 11:14 AM
Subject: [GENERAL] Recursive queries
> I have a database with the following fields:
>
> product_id INTEGER
> parent_id INTEGER
> name VARCHAR(64)
>
> The idea is that it will store a tree of products and its subproducts. Any
> row whose parent_id is NULL will be assumed to be a root product (i.e. top
> level).
>
> This means I will need to process recursively, with some sort of reference
to
> parent_id passing through layers of recursion.
>
> However, I want to save on database queries and do a "SELECT *", then
fetch
> each row on the results recursively, to build the tree. The idea is to
only
> have to do one db query.
>
> Does anyone have any idea how this can be done in PostgreSQL? Both Oracle
and
> DB2 support this with their own proprietary syntax.
>
>
> --
> Juan Jose Comellas
> (juanjo@comellas.org)
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>
>
^ permalink raw reply [nested|flat] 27+ messages in thread
* Recursive queries
@ 2005-01-24 16:27 tmp <skrald@amossen.dk>
2005-01-24 23:30 ` Re: Recursive queries Christopher Browne <cbbrowne@acm.org>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
0 siblings, 2 replies; 27+ messages in thread
From: tmp @ 2005-01-24 16:27 UTC (permalink / raw)
To: pgsql-general
Are there any plans on implementing support for recursive queries in
postgresql in the near future? If so: When?
I can see there has been some discussion on the subject in the
developer-group for quite some time ago, but aparently all thoughts of
recursive queries has been stalled. :-(
Regards
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
@ 2005-01-24 23:30 ` Christopher Browne <cbbrowne@acm.org>
2005-01-25 02:34 ` Re: Recursive queries Jim C. Nasby <decibel@decibel.org>
1 sibling, 1 reply; 27+ messages in thread
From: Christopher Browne @ 2005-01-24 23:30 UTC (permalink / raw)
To: pgsql-general
Quoth tmp <skrald@amossen.dk>:
> Are there any plans on implementing support for recursive queries in
> postgresql in the near future? If so: When?
>
> I can see there has been some discussion on the subject in the
> developer-group for quite some time ago, but aparently all thoughts
> of recursive queries has been stalled. :-(
Other things have headed higher in terms of urgency.
If someone volunteers to work on it, or if someone sponsors someone to
work on it, that would doubtless increase its priority for 8.1 or
such.
I'd love to see it; the syntax provides ways to make complex queries a
lot cleaner even if you're not recursing.
--
select 'cbbrowne' || '@' || 'gmail.com';
http://www.ntlug.org/~cbbrowne/rdbms.html
Be careful when a loop exits to the same place from side and bottom.
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-24 23:30 ` Re: Recursive queries Christopher Browne <cbbrowne@acm.org>
@ 2005-01-25 02:34 ` Jim C. Nasby <decibel@decibel.org>
0 siblings, 0 replies; 27+ messages in thread
From: Jim C. Nasby @ 2005-01-25 02:34 UTC (permalink / raw)
To: Christopher Browne <cbbrowne@acm.org>; +Cc: pgsql-general
On Mon, Jan 24, 2005 at 06:30:52PM -0500, Christopher Browne wrote:
> Quoth tmp <skrald@amossen.dk>:
> > Are there any plans on implementing support for recursive queries in
> > postgresql in the near future? If so: When?
> >
> > I can see there has been some discussion on the subject in the
> > developer-group for quite some time ago, but aparently all thoughts
> > of recursive queries has been stalled. :-(
>
> Other things have headed higher in terms of urgency.
>
> If someone volunteers to work on it, or if someone sponsors someone to
> work on it, that would doubtless increase its priority for 8.1 or
> such.
>
> I'd love to see it; the syntax provides ways to make complex queries a
> lot cleaner even if you're not recursing.
Depending on what you're doing you could also use contrib/ltree instead
of a recursive query.
--
Jim C. Nasby, Database Consultant decibel@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828
Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
@ 2005-01-25 18:58 ` Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 19:24 ` Re: Recursive queries tmp <skrald@amossen.dk>
2005-01-25 19:34 ` Re: Recursive queries Tom Lane <tgl@sss.pgh.pa.us>
2005-01-25 19:36 ` Re: Recursive queries Alvaro Herrera <alvherre@dcc.uchile.cl>
2005-01-26 13:52 ` Re: Recursive queries Christopher Browne <cbbrowne@acm.org>
1 sibling, 4 replies; 27+ messages in thread
From: Martijn van Oosterhout @ 2005-01-25 18:58 UTC (permalink / raw)
To: tmp <skrald@amossen.dk>; +Cc: pgsql-general
On Mon, Jan 24, 2005 at 05:27:46PM +0100, tmp wrote:
> Are there any plans on implementing support for recursive queries in
> postgresql in the near future? If so: When?
>
> I can see there has been some discussion on the subject in the
> developer-group for quite some time ago, but aparently all thoughts of
> recursive queries has been stalled. :-(
What do you mean by resursive queries? A query can have a subquery
which calls a function which executes another query. That counts as
recursion in my book. What type of recursion are you thinking of?
Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
@ 2005-01-25 19:24 ` tmp <skrald@amossen.dk>
2005-01-25 19:50 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
3 siblings, 1 reply; 27+ messages in thread
From: tmp @ 2005-01-25 19:24 UTC (permalink / raw)
To: Martijn van Oosterhout <kleptog@svana.org>; +Cc: pgsql-general
> What do you mean by resursive queries? A query can have a subquery
> which calls a function which executes another query. That counts as
> recursion in my book. What type of recursion are you thinking of?
No, recursion is a pretty well defined term. See
http://en.wikipedia.org/wiki/Recursion
SQL:2003 defines a language construct for recursive queries (T131 and
T132). What I ment with the question was: Will postgresql soon support a
similar (or the same) construct?
I know that some Andrew was assigned the task for a year ago, but
apparently he has been unsubscribed again. :-(
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 19:24 ` Re: Recursive queries tmp <skrald@amossen.dk>
@ 2005-01-25 19:50 ` Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 21:03 ` Re: Recursive queries tmp <skrald@amossen.dk>
0 siblings, 1 reply; 27+ messages in thread
From: Martijn van Oosterhout @ 2005-01-25 19:50 UTC (permalink / raw)
To: tmp <skrald@amossen.dk>; +Cc: pgsql-general
On Tue, Jan 25, 2005 at 08:24:54PM +0100, tmp wrote:
> > What do you mean by resursive queries? A query can have a subquery
> > which calls a function which executes another query. That counts as
> > recursion in my book. What type of recursion are you thinking of?
>
> SQL:2003 defines a language construct for recursive queries (T131 and
> T132). What I ment with the question was: Will postgresql soon support a
> similar (or the same) construct?
I don't have the SQL standard but I think you're referring to tables
that join to themselves and you want to follow these links recursively.
I don't think anybody has written the syntactic sugar, but someone did
write a function that provides equivalent output.
Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 19:24 ` Re: Recursive queries tmp <skrald@amossen.dk>
2005-01-25 19:50 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
@ 2005-01-25 21:03 ` tmp <skrald@amossen.dk>
2005-01-26 23:59 ` Re: Recursive queries PFC <lists@boutiquenumerique.com>
0 siblings, 1 reply; 27+ messages in thread
From: tmp @ 2005-01-25 21:03 UTC (permalink / raw)
To: Martijn van Oosterhout <kleptog@svana.org>; +Cc: pgsql-general
> I don't think anybody has written the syntactic sugar, but someone did
> write a function that provides equivalent output.
I think it is important that the funcionality lies in the database
engine itself: In that way it can more efficiently make use of the
optimizer.
Also, I think this "recursive" feature is *the* most important upcoming
improvements: Currently there are simply no efficient way of fetching
linked structures, which however is quite common in many areas.
Regards
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 19:24 ` Re: Recursive queries tmp <skrald@amossen.dk>
2005-01-25 19:50 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 21:03 ` Re: Recursive queries tmp <skrald@amossen.dk>
@ 2005-01-26 23:59 ` PFC <lists@boutiquenumerique.com>
0 siblings, 0 replies; 27+ messages in thread
From: PFC @ 2005-01-26 23:59 UTC (permalink / raw)
To: pgsql-general
Check out ltree
http://www.sai.msu.su/~megera/postgres/gist/ltree/
On Tue, 25 Jan 2005 22:03:58 +0100, tmp <skrald@amossen.dk> wrote:
>> I don't think anybody has written the syntactic sugar, but someone did
>> write a function that provides equivalent output.
>
> I think it is important that the funcionality lies in the database
> engine itself: In that way it can more efficiently make use of the
> optimizer.
>
> Also, I think this "recursive" feature is *the* most important upcoming
> improvements: Currently there are simply no efficient way of fetching
> linked structures, which however is quite common in many areas.
>
> Regards
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if
> your
> joining column's datatypes do not match
>
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
@ 2005-01-25 19:34 ` Tom Lane <tgl@sss.pgh.pa.us>
3 siblings, 0 replies; 27+ messages in thread
From: Tom Lane @ 2005-01-25 19:34 UTC (permalink / raw)
To: Martijn van Oosterhout <kleptog@svana.org>; +Cc: tmp <skrald@amossen.dk>; pgsql-general
Martijn van Oosterhout <kleptog@svana.org> writes:
> What do you mean by resursive queries?
SQL99 "WITH" syntax. See the archives. Andrew Overholt did some work
in this direction a year or so back, but didn't get real far ...
regards, tom lane
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
@ 2005-01-25 19:36 ` Alvaro Herrera <alvherre@dcc.uchile.cl>
3 siblings, 0 replies; 27+ messages in thread
From: Alvaro Herrera @ 2005-01-25 19:36 UTC (permalink / raw)
To: tmp <skrald@amossen.dk>; pgsql-general
On Tue, Jan 25, 2005 at 07:58:33PM +0100, Martijn van Oosterhout wrote:
> On Mon, Jan 24, 2005 at 05:27:46PM +0100, tmp wrote:
> > Are there any plans on implementing support for recursive queries in
> > postgresql in the near future? If so: When?
> >
> > I can see there has been some discussion on the subject in the
> > developer-group for quite some time ago, but aparently all thoughts of
> > recursive queries has been stalled. :-(
>
> What do you mean by resursive queries? A query can have a subquery
> which calls a function which executes another query. That counts as
> recursion in my book. What type of recursion are you thinking of?
The WITH clause in SQL2003 AFAIR (maybe earlier ones as well).
--
Alvaro Herrera (<alvherre[@]dcc.uchile.cl>)
"Ellos andaban todos desnudos como su madre los parió, y también las mujeres,
aunque no vi más que una, harto moza, y todos los que yo vi eran todos
mancebos, que ninguno vi de edad de más de XXX años" (Cristóbal Colón)
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
@ 2005-01-26 13:52 ` Christopher Browne <cbbrowne@acm.org>
2005-01-26 15:49 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
3 siblings, 1 reply; 27+ messages in thread
From: Christopher Browne @ 2005-01-26 13:52 UTC (permalink / raw)
To: pgsql-general
In an attempt to throw the authorities off his trail, kleptog@svana.org (Martijn van Oosterhout) transmitted:
> On Mon, Jan 24, 2005 at 05:27:46PM +0100, tmp wrote:
>> Are there any plans on implementing support for recursive queries in
>> postgresql in the near future? If so: When?
>>
>> I can see there has been some discussion on the subject in the
>> developer-group for quite some time ago, but aparently all thoughts of
>> recursive queries has been stalled. :-(
>
> What do you mean by resursive queries? A query can have a subquery
> which calls a function which executes another query. That counts as
> recursion in my book. What type of recursion are you thinking of?
By recursive queries, we mean the form defined in SQL3/SQL.1999.
IBM DB2 uses a syntax like the following; I'd have to rummage around
for extra books to verify standards conformance, but hopefully this
gives the idea...
WITH tmp_rel (object, subobject, quantity) AS
(SELECT part, child_part, quantity FROM
partlist root
WHERE root.part in ('ASSEMBLY 1', 'ASSEMBLY 2', 'ASSEMBLY 3')
UNION ALL
SELECT child.part, child.child_part, child.quantity
FROM partlist child, tmp_rel parent
WHERE parent.subobject = child.part)
SELECT DISTINCT object, subobject, quantity
FROM tmp_rel;
What you add in is the "WITH" clause that lets you define a (possibly
self-referencing) query which you then reference below.
This is more or less equivalent to the "let" clause offered in
languages like Lisp and ML
let disc = (x*x-y*y) in
calculate_with_squares (disc)
calculate_again_with_squares (disc);;
Or
(let ((disc (+ (* x x) (* y y))))
(calculate_with_squares disc)
(calculate_again_with_squares disc))
In Lisp, the thing that allows recursing is, strictly speaking, called
"letrec"...
Nonetheless, the similarity is still quite evident. SQL "WITH" allows
building self-referencing queries, as well as allowing you to better
organize bits that are likely to get reused. If I have some complex
subquery that occurs several times in a query, I might want to use
WITH in a not-so-recursive way to factor out that subquery so it only
needs to be expressed once.
--
output = reverse("gro.gultn" "@" "enworbbc")
http://www.ntlug.org/~cbbrowne/linux.html
"Life. Don't talk to me about life."
-- Marvin the Paranoid Android
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive queries
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2005-01-26 13:52 ` Re: Recursive queries Christopher Browne <cbbrowne@acm.org>
@ 2005-01-26 15:49 ` Martijn van Oosterhout <kleptog@svana.org>
0 siblings, 0 replies; 27+ messages in thread
From: Martijn van Oosterhout @ 2005-01-26 15:49 UTC (permalink / raw)
To: Christopher Browne <cbbrowne@acm.org>; +Cc: pgsql-general
On Wed, Jan 26, 2005 at 08:52:37AM -0500, Christopher Browne wrote:
> By recursive queries, we mean the form defined in SQL3/SQL.1999.
>
> IBM DB2 uses a syntax like the following; I'd have to rummage around
> for extra books to verify standards conformance, but hopefully this
> gives the idea...
>
> WITH tmp_rel (object, subobject, quantity) AS
> (SELECT part, child_part, quantity FROM
> partlist root
> WHERE root.part in ('ASSEMBLY 1', 'ASSEMBLY 2', 'ASSEMBLY 3')
> UNION ALL
> SELECT child.part, child.child_part, child.quantity
> FROM partlist child, tmp_rel parent
> WHERE parent.subobject = child.part)
> SELECT DISTINCT object, subobject, quantity
> FROM tmp_rel;
>
> What you add in is the "WITH" clause that lets you define a (possibly
> self-referencing) query which you then reference below.
OMG! While I can understand what you say query does, I simply can't
visualise it at all. Using WITH for named subqueries, that I can
understand, it would even be useful. But self-referencing, I can't even
think of how an executor would even calculate the resultset of the
above query.
There must be some additional constraints, because what happens if I
write a query like:
WITH tmp_rel (num) AS
(SELECT num+1 FROM tmp_rel)
SELECT * FROM tmp_rel;
If I'm understanding you correctly, this query should never complete. I
guess you need to build a query processor smart enough to detect this.
As a base recursive functions should have a seed and an iterator and
the syntax should reflect that. I don't think the WITH syntax is doing
that.
Incidently, for the case people are pointing to recursive queries here,
the contrib/tablefunc module provides an answer with the connectby().
Given the table name, the parent and the child field names it will
return a set resulting from a walk down the tree:
http://developer.postgresql.org/cvsweb.cgi/~checkout~/pgsql/contrib/tablefunc/README.tablefunc?rev=1...
Anyway, thanks for the info about recurive queries.
Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.
^ permalink raw reply [nested|flat] 27+ messages in thread
* Recursive Queries
@ 2020-04-16 08:35 Alex Magnum <magnum11200@gmail.com>
2020-04-16 12:48 ` RE: Recursive Queries Rob Northcott <Rob.Northcott@compilator.com>
2020-04-16 13:36 ` Re: Recursive Queries Edward Macnaghten <eddy@edlsystems.com>
2020-04-16 13:42 ` Re: Recursive Queries Olivier Gautherot <ogautherot@gautherot.net>
0 siblings, 3 replies; 27+ messages in thread
From: Alex Magnum @ 2020-04-16 08:35 UTC (permalink / raw)
To: pgsql-general
Hi,
I have a simple table with singup timestamps
What I would like to do is to create a table as shown below that displays
the counts per our for the past n dates.
I can do this with a function but is there an easy way to use recursive
queries?
* Counts per hour for given date*
*HR 2020-04-01 2020-04-02 ... 2020-04-10*00 38 33
36
01 33 26 18
02 26 36 17
03 36 18 10
04 18 17 3
05 17 10 3
06 10 3 6
07 3 3 10
. 3 6 13
. 6 10 22
. 10 13 12
22 13 22 9
23 22 11 8
Thanks for any suggestions.
A
^ permalink raw reply [nested|flat] 27+ messages in thread
* RE: Recursive Queries
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
@ 2020-04-16 12:48 ` Rob Northcott <Rob.Northcott@compilator.com>
2020-04-16 13:24 ` Re: Recursive Queries Julien Rouhaud <rjuju123@gmail.com>
2020-04-16 13:25 ` Re: Recursive Queries Michael Lewis <mlewis@entrata.com>
2 siblings, 2 replies; 27+ messages in thread
From: Rob Northcott @ 2020-04-16 12:48 UTC (permalink / raw)
To: pgsql-general
From: Alex Magnum <magnum11200@gmail.com>
Sent: 16 April 2020 09:36
To: Postgres General <pgsql-general@postgresql.org>
Subject: Recursive Queries
Hi,
I have a simple table with singup timestamps
What I would like to do is to create a table as shown below that displays the counts per our for the past n dates.
I can do this with a function but is there an easy way to use recursive queries?
Counts per hour for given date
HR 2020-04-01 2020-04-02 ... 2020-04-10
00 38 33 36
01 33 26 18
02 26 36 17
03 36 18 10
04 18 17 3
05 17 10 3
06 10 3 6
07 3 3 10
. 3 6 13
. 6 10 22
. 10 13 12
22 13 22 9
23 22 11 8
Thanks for any suggestions.
A
Wouldn’t it be easier to do it the other way round, with a column per hour and a row per date? That way the query just needs one sub-query per hour to select just the count for that hour, and group by date.
Rob
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive Queries
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
2020-04-16 12:48 ` RE: Recursive Queries Rob Northcott <Rob.Northcott@compilator.com>
@ 2020-04-16 13:24 ` Julien Rouhaud <rjuju123@gmail.com>
1 sibling, 0 replies; 27+ messages in thread
From: Julien Rouhaud @ 2020-04-16 13:24 UTC (permalink / raw)
To: Rob Northcott <Rob.Northcott@compilator.com>; +Cc: pgsql-general
On Thu, Apr 16, 2020 at 2:49 PM Rob Northcott
<Rob.Northcott@compilator.com> wrote:
>
> From: Alex Magnum <magnum11200@gmail.com>
>
> What I would like to do is to create a table as shown below that displays the counts per our for the past n dates.
>
>
>
> I can do this with a function but is there an easy way to use recursive queries?
>
> Counts per hour for given date
>
> HR 2020-04-01 2020-04-02 ... 2020-04-10
> 00 38 33 36
> 01 33 26 18
> 02 26 36 17
> 03 36 18 10
> 04 18 17 3
> 05 17 10 3
> 06 10 3 6
> 07 3 3 10
> . 3 6 13
> . 6 10 22
> . 10 13 12
> 22 13 22 9
> 23 22 11 8
>
> Wouldn’t it be easier to do it the other way round, with a column per hour and a row per date? That way the query just needs one sub-query per hour to select just the count for that hour, and group by date.
Note that you wouldn't need subqueries for that, the FILTER clause can
be used and is supported since version 9.4.
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive Queries
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
2020-04-16 12:48 ` RE: Recursive Queries Rob Northcott <Rob.Northcott@compilator.com>
@ 2020-04-16 13:25 ` Michael Lewis <mlewis@entrata.com>
1 sibling, 0 replies; 27+ messages in thread
From: Michael Lewis @ 2020-04-16 13:25 UTC (permalink / raw)
To: Rob Northcott <Rob.Northcott@compilator.com>; +Cc: pgsql-general
You don't want recursion, you want pivot table (Excel) behavior to reformat
rows into columns. The easiest way to get this data in its raw form would
be to group by date and hour of day and compute the count.
If you have the option to add extensions in your environment, then you
should be able to pivot your data pretty simply.
https://www.postgresql.org/docs/12/tablefunc.html
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive Queries
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
@ 2020-04-16 13:36 ` Edward Macnaghten <eddy@edlsystems.com>
2020-04-16 13:45 ` Re: Recursive Queries Edward Macnaghten <eddy@edlsystems.com>
2 siblings, 1 reply; 27+ messages in thread
From: Edward Macnaghten @ 2020-04-16 13:36 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 16/04/2020 09:35, Alex Magnum wrote:
> Hi,
> I have a simple table with singup timestamps
>
> What I would like to do is to create a table as shown below that
> displays the counts per our for the past n dates.
Various ways, but for me...
SELECT hour, SUM(CASE(WHEN date = date THEN 1 ELSE 0)), ...., ....
WHERE whatever
GROUP BY hour
could be your friend
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive Queries
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
2020-04-16 13:36 ` Re: Recursive Queries Edward Macnaghten <eddy@edlsystems.com>
@ 2020-04-16 13:45 ` Edward Macnaghten <eddy@edlsystems.com>
2020-04-16 18:06 ` Re: Recursive Queries Alex Magnum <magnum11200@gmail.com>
0 siblings, 1 reply; 27+ messages in thread
From: Edward Macnaghten @ 2020-04-16 13:45 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 16/04/2020 14:36, Edward Macnaghten wrote:
> On 16/04/2020 09:35, Alex Magnum wrote:
>> Hi,
>> I have a simple table with singup timestamps
>>
>> What I would like to do is to create a table as shown below that
>> displays the counts per our for the past n dates.
SELECT hour, SUM(CASE(WHEN date = date THEN 1 ELSE 0 END)), ...., ...
WHERE whatever
GROUP BY hour
Could be your friend
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive Queries
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
2020-04-16 13:36 ` Re: Recursive Queries Edward Macnaghten <eddy@edlsystems.com>
2020-04-16 13:45 ` Re: Recursive Queries Edward Macnaghten <eddy@edlsystems.com>
@ 2020-04-16 18:06 ` Alex Magnum <magnum11200@gmail.com>
0 siblings, 0 replies; 27+ messages in thread
From: Alex Magnum @ 2020-04-16 18:06 UTC (permalink / raw)
To: Edward Macnaghten <eddy@edlsystems.com>; +Cc: pgsql-general@lists.postgresql.org
thanks for the suggestion. tablefunc extension might be the easiest one
On Thu, Apr 16, 2020 at 9:46 PM Edward Macnaghten <eddy@edlsystems.com>
wrote:
> On 16/04/2020 14:36, Edward Macnaghten wrote:
> > On 16/04/2020 09:35, Alex Magnum wrote:
> >> Hi,
> >> I have a simple table with singup timestamps
> >>
> >> What I would like to do is to create a table as shown below that
> >> displays the counts per our for the past n dates.
> SELECT hour, SUM(CASE(WHEN date = date THEN 1 ELSE 0 END)), ...., ...
> WHERE whatever
> GROUP BY hour
>
> Could be your friend
>
>
>
>
^ permalink raw reply [nested|flat] 27+ messages in thread
* Re: Recursive Queries
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
@ 2020-04-16 13:42 ` Olivier Gautherot <ogautherot@gautherot.net>
2 siblings, 0 replies; 27+ messages in thread
From: Olivier Gautherot @ 2020-04-16 13:42 UTC (permalink / raw)
To: Alex Magnum <magnum11200@gmail.com>; +Cc: pgsql-general
Hi Alex,
On Thu, Apr 16, 2020 at 10:36 AM Alex Magnum <magnum11200@gmail.com> wrote:
> Hi,
> I have a simple table with singup timestamps
>
> What I would like to do is to create a table as shown below that displays
> the counts per our for the past n dates.
>
> I can do this with a function but is there an easy way to use recursive
> queries?
>
>
> * Counts per hour for given date*
>
> *HR 2020-04-01 2020-04-02 ... 2020-04-10*00 38 33
> 36
> 01 33 26 18
> 02 26 36 17
> 03 36 18 10
> 04 18 17 3
> 05 17 10 3
> 06 10 3 6
> 07 3 3 10
> . 3 6 13
> . 6 10 22
> . 10 13 12
> 22 13 22 9
> 23 22 11 8
>
> Thanks for any suggestions.
> A
>
You don't need subqueries. The WHEN statement can help you in this case (a
bit tedious to write but fast to run):
WITH q AS (select date_part('hour', ts) AS hr, ts::date AS mydate FROM
your_table_or_query)
SELECT hr,
sum(CASE WHEN mydate = '2020-04-01'::date THEN 1 ELSE 0 END),
sum(CASE WHEN mydate = '2020-04-02'::date THEN 1 ELSE 0 END),
...
FROM q ORDER BY hr;
Hope it helps
--
Olivier Gautherot
Tel: +33 6 02 71 92 23
https://www.linkedin.com/in/ogautherot/
^ permalink raw reply [nested|flat] 27+ messages in thread
end of thread, other threads:[~2020-04-16 18:06 UTC | newest]
Thread overview: 27+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-04-09 16:25 recursive queries? Ron Peterson <rpeterson@yellowbank.com>
2000-04-10 13:48 ` Andrew Schmeder <andy@qabobala.xapnet.com>
2000-04-10 20:45 ` Peter Eisentraut <peter_e@gmx.net>
2000-04-11 15:21 ` Ron Peterson <rpeterson@yellowbank.com>
2000-09-14 14:45 Recursive queries Rob Hoopman <uithuis@dds.nl>
2001-09-21 16:14 Recursive queries Juan Jose Comellas <juanjo@comellas.org>
2001-09-21 18:46 ` Re: Recursive queries Jeff Eckermann <jeckermann@verio.net>
2005-01-24 16:27 Recursive queries tmp <skrald@amossen.dk>
2005-01-24 23:30 ` Re: Recursive queries Christopher Browne <cbbrowne@acm.org>
2005-01-25 02:34 ` Re: Recursive queries Jim C. Nasby <decibel@decibel.org>
2005-01-25 18:58 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 19:24 ` Re: Recursive queries tmp <skrald@amossen.dk>
2005-01-25 19:50 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2005-01-25 21:03 ` Re: Recursive queries tmp <skrald@amossen.dk>
2005-01-26 23:59 ` Re: Recursive queries PFC <lists@boutiquenumerique.com>
2005-01-25 19:34 ` Re: Recursive queries Tom Lane <tgl@sss.pgh.pa.us>
2005-01-25 19:36 ` Re: Recursive queries Alvaro Herrera <alvherre@dcc.uchile.cl>
2005-01-26 13:52 ` Re: Recursive queries Christopher Browne <cbbrowne@acm.org>
2005-01-26 15:49 ` Re: Recursive queries Martijn van Oosterhout <kleptog@svana.org>
2020-04-16 08:35 Recursive Queries Alex Magnum <magnum11200@gmail.com>
2020-04-16 12:48 ` RE: Recursive Queries Rob Northcott <Rob.Northcott@compilator.com>
2020-04-16 13:24 ` Re: Recursive Queries Julien Rouhaud <rjuju123@gmail.com>
2020-04-16 13:25 ` Re: Recursive Queries Michael Lewis <mlewis@entrata.com>
2020-04-16 13:36 ` Re: Recursive Queries Edward Macnaghten <eddy@edlsystems.com>
2020-04-16 13:45 ` Re: Recursive Queries Edward Macnaghten <eddy@edlsystems.com>
2020-04-16 18:06 ` Re: Recursive Queries Alex Magnum <magnum11200@gmail.com>
2020-04-16 13:42 ` Re: Recursive Queries Olivier Gautherot <ogautherot@gautherot.net>
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