473,769 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hierarchical queries

Hello everybody!

Does someone know how to build hierarchical queries to the postgresql?

I have a table with tree in it (id, parent)
and need to find a way from any point of the tree to any other point.
And i would like to have a list of all steps from point A to point B
to make some changes on each step (this is required by the algorythm).

Here is an example:
treetable (where tree is stored):
id parent data
int4 int4 varchar(255)
0 0 root
1 0 root's chield 1
2 0 root's chield 2
3 1 root's chield 1 chield 1
4 1 root's chield 1 chield 2
5 2 root's chield 2 chield 1
6 2 root's chield 2 chield 2

And i want to get something like this:
start point "root's chield 2 chield 2"
finish "root's chield 1 chield 1"

And the result i need:
id parent data
6 2 root's chield 2 chield 2
2 0 root's chield 2
0 0 root
1 0 root's chield 1
4 1 root's chield 1 chield 2

i know that it is possible in Oracle but what about postgres?

Best regards,
Anton Nikiforov
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #1
13 5903

There's a patch to mimic Oracle's CONNECT BY queries. You can get it
at the Postgres Cookbook site:

http://www.brasileiro.net/postgres/cookbook.

(although it seems to be down at the moment...)
On Jan 9, 2004, at 2:05 PM, An************* @loteco.ru wrote:
Hello everybody!

Does someone know how to build hierarchical queries to the postgresql?

I have a table with tree in it (id, parent)
and need to find a way from any point of the tree to any other point.
And i would like to have a list of all steps from point A to point B
to make some changes on each step (this is required by the algorythm).

Here is an example:
treetable (where tree is stored):
id parent data
int4 int4 varchar(255)
0 0 root
1 0 root's chield 1
2 0 root's chield 2
3 1 root's chield 1 chield 1
4 1 root's chield 1 chield 2
5 2 root's chield 2 chield 1
6 2 root's chield 2 chield 2

And i want to get something like this:
start point "root's chield 2 chield 2"
finish "root's chield 1 chield 1"

And the result i need:
id parent data
6 2 root's chield 2 chield 2
2 0 root's chield 2
0 0 root
1 0 root's chield 1
4 1 root's chield 1 chield 2

i know that it is possible in Oracle but what about postgres?

Best regards,
Anton Nikiforov
---------------------------(end of
broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

--------------------

Andrew Rawnsley
President
The Ravensfield Digital Resource Group, Ltd.
(740) 587-0114
www.ravensfield.com
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
On Friday 09 January 2004 19:16, Andrew Rawnsley wrote:
There's a patch to mimic Oracle's CONNECT BY queries. You can get it
at the Postgres Cookbook site:

http://www.brasileiro.net/postgres/cookbook.


I believe I saw an announcement on freshmeat about a patch for the source to
allow Oracle-style connect by. Yep:
http://gppl.terminal.ru/index.eng.html

I could have sworn there was something in contrib/ too, but I can't see it
now.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #3
Look at contrib/ltree
http://www.sai.msu.su/~megera/postgres/gist/ltree

Oleg
On Fri, 9 Jan 2004 An************* @loteco.ru wrote:
Hello everybody!

Does someone know how to build hierarchical queries to the postgresql?

I have a table with tree in it (id, parent)
and need to find a way from any point of the tree to any other point.
And i would like to have a list of all steps from point A to point B
to make some changes on each step (this is required by the algorythm).

Here is an example:
treetable (where tree is stored):
id parent data
int4 int4 varchar(255)
0 0 root
1 0 root's chield 1
2 0 root's chield 2
3 1 root's chield 1 chield 1
4 1 root's chield 1 chield 2
5 2 root's chield 2 chield 1
6 2 root's chield 2 chield 2

And i want to get something like this:
start point "root's chield 2 chield 2"
finish "root's chield 1 chield 1"

And the result i need:
id parent data
6 2 root's chield 2 chield 2
2 0 root's chield 2
0 0 root
1 0 root's chield 1
4 1 root's chield 1 chield 2

i know that it is possible in Oracle but what about postgres?

Best regards,
Anton Nikiforov
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Regards,
Oleg
_______________ _______________ _______________ _______________ _
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: ol**@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 12 '05 #4
Hello and thanks for the links, but
http://www.brasileiro.net/postgres/cookbook.

this site is still down or at least do not accsepting requests, and
RH> http://gppl.terminal.ru/index.eng.html
this patch is not working with my 7.4 release, i tried hier-0.3, but
cannot compile my postgres with it installed.
RH> I could have sworn there was something in contrib/ too, but I can't see it
RH> now.
Yes it is gone. :)
One more URL:
http://www.sai.msu.su/~megera/postgres/gist/ltree
I read all but did not get how to get a tree sorted starting not from
root, but from required started point of the tree getting a full path
to the required finish.

Best regards,
Anton Nikiforov.
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #5
Hello Oleg and thanks for the link, but i could not understand how to
get path from one point of the tree to another?
Anyway thanks :)

Best regards,
Anton
OB> Look at contrib/ltree
OB> http://www.sai.msu.su/~megera/postgres/gist/ltree

OB> Oleg
OB> On Fri, 9 Jan 2004 An************* @loteco.ru wrote:
Hello everybody!

Does someone know how to build hierarchical queries to the postgresql?

I have a table with tree in it (id, parent)
and need to find a way from any point of the tree to any other point.
And i would like to have a list of all steps from point A to point B
to make some changes on each step (this is required by the algorythm).

Here is an example:
treetable (where tree is stored):
id parent data
int4 int4 varchar(255)
0 0 root
1 0 root's chield 1
2 0 root's chield 2
3 1 root's chield 1 chield 1
4 1 root's chield 1 chield 2
5 2 root's chield 2 chield 1
6 2 root's chield 2 chield 2

And i want to get something like this:
start point "root's chield 2 chield 2"
finish "root's chield 1 chield 1"

And the result i need:
id parent data
6 2 root's chield 2 chield 2
2 0 root's chield 2
0 0 root
1 0 root's chield 1
4 1 root's chield 1 chield 2

i know that it is possible in Oracle but what about postgres?

Best regards,
Anton Nikiforov
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


OB> Regards,
OB> Oleg
OB> _______________ _______________ _______________ _______________ _
OB> Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
OB> Sternberg Astronomical Institute, Moscow University (Russia)
OB> Internet: ol**@sai.msu.su, http://www.sai.msu.su/~megera/
OB> phone: +007(095)939-16-83, +007(095)939-23-83

OB> ---------------------------(end of broadcast)---------------------------
OB> TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #6
On Sat, 10 Jan 2004 An************* @loteco.ru wrote:
Hello Oleg and thanks for the link, but i could not understand how to
get path from one point of the tree to another?
have you read documentation ? Get all childrens - ltree <@ ltree,
for example:

ltreetest=# select path from test where path <@ 'Top.Science';
path
------------------------------------
Top.Science
Top.Science.Ast ronomy
Top.Science.Ast ronomy.Astrophy sics
Top.Science.Ast ronomy.Cosmolog y
(4 rows)

You should provide us example of your data and query, so we could help you.
Anyway thanks :)

Best regards,
Anton
OB> Look at contrib/ltree
OB> http://www.sai.msu.su/~megera/postgres/gist/ltree

OB> Oleg
OB> On Fri, 9 Jan 2004 An************* @loteco.ru wrote:
Hello everybody!

Does someone know how to build hierarchical queries to the postgresql?

I have a table with tree in it (id, parent)
and need to find a way from any point of the tree to any other point.
And i would like to have a list of all steps from point A to point B
to make some changes on each step (this is required by the algorythm).

Here is an example:
treetable (where tree is stored):
id parent data
int4 int4 varchar(255)
0 0 root
1 0 root's chield 1
2 0 root's chield 2
3 1 root's chield 1 chield 1
4 1 root's chield 1 chield 2
5 2 root's chield 2 chield 1
6 2 root's chield 2 chield 2

And i want to get something like this:
start point "root's chield 2 chield 2"
finish "root's chield 1 chield 1"

And the result i need:
id parent data
6 2 root's chield 2 chield 2
2 0 root's chield 2
0 0 root
1 0 root's chield 1
4 1 root's chield 1 chield 2

i know that it is possible in Oracle but what about postgres?

Best regards,
Anton Nikiforov
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


OB> Regards,
OB> Oleg
OB> _______________ _______________ _______________ _______________ _
OB> Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
OB> Sternberg Astronomical Institute, Moscow University (Russia)
OB> Internet: ol**@sai.msu.su, http://www.sai.msu.su/~megera/
OB> phone: +007(095)939-16-83, +007(095)939-23-83

OB> ---------------------------(end of broadcast)---------------------------
OB> TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster


Regards,
Oleg
_______________ _______________ _______________ _______________ _
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: ol**@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 12 '05 #7
Hello Oleg!
There is no data yet, i'm just planning to start a new project :)
Text labels are just fine and i red the documentation from the top to
the very end a few times and found the way to use your module, but
using it will not as beautiful as i was planning mathematicaly.
You know i have (planning to have) a tree like:
>> id parent data
>> int4 int4 varchar(255)
>> 0 0 root
>> 1 0 root's chield 1
>> 2 0 root's chield 2
>> 3 1 root's chield 1 chield 1
>> 4 1 root's chield 1 chield 2
>> 5 2 root's chield 2 chield 1
>> 6 2 root's chield 2 chield 2
And to find a way from the record with id #6 to the record with id #3
WITH YOUR MODULE:
I have to find Lowest Common Ancestor (lca)
Then to find a path from id #6 to lca
Then to find a path from lca to id#3
Then combine this pathes (remember that i need all steps from id #6 to
id #3)
And then run a special code to update all needed data (create records
in different tables)

IN MY BRAINS:
I just need to have function that will rotate a tree and make id #6
the root element and then select a path from root (id#6) to desired id
#3. As i think somebody did this already. And i'm not the first who is
trying to find out the code.

If i'm too stupid to understand the ability of your module - just give
me a direction (i did installed your module and currently playing with
it, so maybe my stupidity will become wiser and wiser in the nearest
feature) :))))

Best regards,
Anton
OB> On Sat, 10 Jan 2004 An************* @loteco.ru wrote:
Hello Oleg and thanks for the link, but i could not understand how to
get path from one point of the tree to another?
OB> have you read documentation ? Get all childrens - ltree <@ ltree,
OB> for example:

OB> ltreetest=# select path from test where path <@ 'Top.Science';
OB> path
OB> ------------------------------------
OB> Top.Science
OB> Top.Science.Ast ronomy
OB> Top.Science.Ast ronomy.Astrophy sics
OB> Top.Science.Ast ronomy.Cosmolog y
OB> (4 rows)

OB> You should provide us example of your data and query, so we could help you.
Anyway thanks :)

Best regards,
Anton
OB> Look at contrib/ltree
OB> http://www.sai.msu.su/~megera/postgres/gist/ltree

OB> Oleg
OB> On Fri, 9 Jan 2004 An************* @loteco.ru wrote:
>> Hello everybody!
>>
>> Does someone know how to build hierarchical queries to the postgresql?
>>
>> I have a table with tree in it (id, parent)
>> and need to find a way from any point of the tree to any other point.
>> And i would like to have a list of all steps from point A to point B
>> to make some changes on each step (this is required by the algorythm).
>>
>> Here is an example:
>> treetable (where tree is stored):
>> id parent data
>> int4 int4 varchar(255)
>> 0 0 root
>> 1 0 root's chield 1
>> 2 0 root's chield 2
>> 3 1 root's chield 1 chield 1
>> 4 1 root's chield 1 chield 2
>> 5 2 root's chield 2 chield 1
>> 6 2 root's chield 2 chield 2
>>
>> And i want to get something like this:
>> start point "root's chield 2 chield 2"
>> finish "root's chield 1 chield 1"
>>
>> And the result i need:
>> id parent data
>> 6 2 root's chield 2 chield 2
>> 2 0 root's chield 2
>> 0 0 root
>> 1 0 root's chield 1
>> 4 1 root's chield 1 chield 2
>>
>> i know that it is possible in Oracle but what about postgres?
>>
>> Best regards,
>> Anton Nikiforov
>>
>>
>> ---------------------------(end of broadcast)---------------------------
>> TIP 7: don't forget to increase your free space map settings
>>


OB> Regards,
OB> Oleg
OB> _______________ _______________ _______________ _______________ _
OB> Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
OB> Sternberg Astronomical Institute, Moscow University (Russia)
OB> Internet: ol**@sai.msu.su, http://www.sai.msu.su/~megera/
OB> phone: +007(095)939-16-83, +007(095)939-23-83

OB> ---------------------------(end of broadcast)---------------------------
OB> TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster


OB> Regards,
OB> Oleg
OB> _______________ _______________ _______________ _______________ _
OB> Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
OB> Sternberg Astronomical Institute, Moscow University (Russia)
OB> Internet: ol**@sai.msu.su, http://www.sai.msu.su/~megera/
OB> phone: +007(095)939-16-83, +007(095)939-23-83
Ñ óâàæåíèåì,
IT Äèðåêòîð ÎÎÎ "Ëîòýêî"
Àíòîí Íèêèôîðîâ
Òåë.: +7 095 7814200
Ôàêñ: +7 095 7814201
Mail: An************* @loteco.ru
Web: www.loteco.ru
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #8
An************* @loteco.ru wrote:
RH> I could have sworn there was something in contrib/ too, but I can't see it
RH> now.
Yes it is gone. :)


See contrib/tablefunc for a function called connectby().

Joe
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #9
Hi alltogether

I have a table with two fields, d1 timestamp and dur smallint.
d1 is the starting date and dur is the duration. From this two fields
I want to generate future dates for the whole table.
There is no problem with queries where a number for the duration is
given.

test=# select d1,dur,d1 + '6 month' from t1;
d1 | dur | ?column?
-----------------------+-----+---------------------
2003-12-27 00:00:00 | 4 | 2004-06-27 00:00:00
2003-11-14 00:00:00 | 7 | 2004-05-14 00:00:00
2004-01-03 00:00:00 | 5 | 2004-07-03 00:00:00

I want to have the date plus the duration stored in the table, but
didn't succeed.

test=# select '\''||dur::varc har||' month\'' from t1;
?column?
-----------
'4 month'
'7 month'
'5 month'

but

test=# select d1 + '\''||wielange: :varchar||' month\'' from t1;
ERROR: invalid input syntax for type interval: "'"

Any hints are welcome

Regards
Conni
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2047
by: Reimar Bauer | last post by:
Hi all, I would like to use a hierarchical group oriented encryption. Is there something implemented or did you know something I could use? For explanaition. If you have a large building there are several keys available. Each person has a key to open his/her room. Probably this key is able to open rooms of the group of this person.
1
3682
by: ALEX KLEIN | last post by:
I want to use fabricated hierarchical recordset in VB6 using ADO. I wrote code like dim rs as adodb.recordest set rs=new adodb.recordest rs.fields.append "a1",adChar,30 Then in loop I put rs.addnew rs("a1")=... re.update when I associated this with hierarchical flexgrid I saw what I expected. On
0
4121
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting together a query to pull out an alternative hierarchical view of my data. The database is implemented under SQL Sever 2000 and I am writing the front end using VB.Net and ADO.net. The following is the portion of my database structure that I am...
3
5588
by: Cláudia Morgado | last post by:
Hello! Oracle has the option with the SQL CONECT BY statement to run through a hierarchical database with a single SQl-statement: <!--SQL SELECT ms_id,ms_parent FROM messages CONNECT BY PRIOR ms_id = ms_parent START WITH ms_id = 1 --> Result-set (example): ms_id parent_id 1 1.1 1 1.1.1 1.1 1.1.2 1.1 1.1.3 1.1 1.2 1 1.2.1 1.2
4
2609
by: Daisy | last post by:
Let's say I've got a forum, where users can be moderators of each forum. Tables look like this: USER -------- user_key name FORUM
5
2257
by: clintonG | last post by:
I'm looking for documentation and would not turn my nose up to any code from anybody who thinks they are good at the design of an algorythm that can be used to generated a hierarchical relational data model. What? A Yahoo-like drill-down menu that is a series of categories and nested categories is a hierarchical relational data model. An example can be seen at but the review of the query string values strongly indicates
3
2043
by: Bennett Haselton | last post by:
I want to display a hierarchical listing of items from a database table, where, say, each row in the table has an "ID" field and a "parent_id" field giving the ID of its parent (NULL if it's at the top level of the hierarchy) -- like message posts and their replies. Is there a built-in way to do this, or a generally accepted simplest way? My first idea was to create a user control like HierarchicalListing that contains a Repeater, and...
12
5823
by: Steve | last post by:
I have been studying the Adjacency List Model as a means of achieving a folder structure in a project I am working on. Started with the excellent article by Gijs Van Tulder http://www.sitepoint.com/article/hierarchical-data-database My database has this basic structure: Id FolderName
30
2368
by: Vadim Tropashko | last post by:
Reposting with more clarification (as Jan asked). Suppose I have a BNFgrammar and a source text parsed into a tree. How would I query an identifier declaration? All the XQuery tutorials (where I went to gather some ideas) start with simpleminded examples like browsing all the descendants of / bookstore/book (and the bookstore XML design is such wrong idea to boot).
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.