Thread: SQL Query for Top Down fetching of childs
Dear Friends,
Postgres 7.3.4 on RH Linux 7.2.
I need a query to get the Childs of a parent (Top down analysis). Need to list all the departments(Childs) of a parent organization. The table structure is
CREATE TABLE organization
(
entity_id int4,
entity_name varchar(100),
entity_type varchar(25),
parent_entity_id int4,
) WITH OIDS;
(
entity_id int4,
entity_name varchar(100),
entity_type varchar(25),
parent_entity_id int4,
) WITH OIDS;
A parent can have n number of Childs. So I need to list all the childs for a parent.
For example I query the Division , then it lists it Childs
# select * from organization where parent_entity_id = 3;
entity_id | entity_name | entity_type | parent_entity_id
-----------+-------------+-----------------+------------------
5 | HR | EngineeringTeam | 3
12 | PM | EngineeringTeam | 3
8 | Finance | Dept | 3
6 | Quality | Dept | 3
entity_id | entity_name | entity_type | parent_entity_id
-----------+-------------+-----------------+------------------
5 | HR | EngineeringTeam | 3
12 | PM | EngineeringTeam | 3
8 | Finance | Dept | 3
6 | Quality | Dept | 3
I need to drill down to the last level Engineering Team in this example.
So I query entity_id 8 further, it gives me its Childs
=# select * from organization where parent_entity_id = 8;
entity_id | entity_name | entity_type | parent_entity_id
-----------+-------------+-------------+------------------
15 | Audit | Group | 8
16 | Mkt | Group | 8
(2 rows)
entity_id | entity_name | entity_type | parent_entity_id
-----------+-------------+-------------+------------------
15 | Audit | Group | 8
16 | Mkt | Group | 8
(2 rows)
Again, I need to query the entity_id 15 to get its child
=# select * from organization where parent_entity_id = 15;
entity_id | entity_name | entity_type | parent_entity_id
-----------+-------------+-----------------+------------------
17 | CA | EngineeringTeam | 15
18 | Comm | EngineeringTeam | 15
19 | EComm | EngineeringTeam | 15
(3 rows)
entity_id | entity_name | entity_type | parent_entity_id
-----------+-------------+-----------------+------------------
17 | CA | EngineeringTeam | 15
18 | Comm | EngineeringTeam | 15
19 | EComm | EngineeringTeam | 15
(3 rows)
I have used the following query, but not useful.
CREATE OR REPLACE FUNCTION.fn_get_all_organization(int4)
RETURNS SETOF organization AS
'DECLARE
pi_entity_id ALIAS FOR $1;
rec_result organization%ROWTYPE;
rec_proc organization%ROWTYPE;
v_patent_entity_id INT;
RETURNS SETOF organization AS
'DECLARE
pi_entity_id ALIAS FOR $1;
rec_result organization%ROWTYPE;
rec_proc organization%ROWTYPE;
v_patent_entity_id INT;
BEGIN
FOR rec_result IN SELECT entity_id, entity_name, entity_type, parent_entity_id FROM organization ben
WHERE ben.parent_entity_id = pi_entity_id
FOR rec_result IN SELECT entity_id, entity_name, entity_type, parent_entity_id FROM organization ben
WHERE ben.parent_entity_id = pi_entity_id
LOOP
IF rec_result.entity_type = \'EngineeingTeam\' THEN
RETURN NEXT rec_result;
ELSE
v_patent_entity_id := rec_result.entity_id;
LOOP
FOR rec_proc IN SELECT bse.entity_id, bse.entity_name, bse.entity_type, bse.parent_entity_id FROM organization bse
WHERE bse.parent_entity_id= v_patent_entity_id
IF rec_result.entity_type = \'EngineeingTeam\' THEN
RETURN NEXT rec_result;
ELSE
v_patent_entity_id := rec_result.entity_id;
LOOP
FOR rec_proc IN SELECT bse.entity_id, bse.entity_name, bse.entity_type, bse.parent_entity_id FROM organization bse
WHERE bse.parent_entity_id= v_patent_entity_id
LOOP
IF rec_proc.entity_type = \'EngineeringTeam\' THEN
RETURN NEXT rec_proc;
ELSE
v_patent_entity_id := rec_proc.entity_id;
END IF;
END LOOP;
EXIT WHEN rec_proc.entity_type = \'EngineeringTeam\';
END LOOP;
END IF;
END LOOP;
RETURN;
END;'
LANGUAGE 'plpgsql' VOLATILE;
IF rec_proc.entity_type = \'EngineeringTeam\' THEN
RETURN NEXT rec_proc;
ELSE
v_patent_entity_id := rec_proc.entity_id;
END IF;
END LOOP;
EXIT WHEN rec_proc.entity_type = \'EngineeringTeam\';
END LOOP;
END IF;
END LOOP;
RETURN;
END;'
LANGUAGE 'plpgsql' VOLATILE;
Anybody pls help me with this. I am first time writing these kind of function for TOP DOWN analysis. Please shed light.
Regards
Senthil Kumar S
On Thursday 29 January 2004 06:11, Kumar wrote: > Dear Friends, > > Postgres 7.3.4 on RH Linux 7.2. > > I need a query to get the Childs of a parent (Top down analysis). Need to > list all the departments(Childs) of a parent organization. The table > structure is Two places to look for examples: 1. the contrib/tablefunc folder has an example of this sort of thing 2. search the mailing list articles for CONNECT BY (oracle's name for this sort of thing) or "tree" -- Richard Huxton Archonet Ltd
Thanks a lot Mr.Richard Huxton. It managed to find a similar one and modified to my need. It is working fine. Thanks a lot ----- Original Message ----- From: "Richard Huxton" <dev@archonet.com> To: "Kumar" <sgnerd@yahoo.com.sg>; "psql" <pgsql-sql@postgresql.org> Sent: Thursday, January 29, 2004 3:57 PM Subject: Re: [SQL] SQL Query for Top Down fetching of childs > On Thursday 29 January 2004 06:11, Kumar wrote: > > Dear Friends, > > > > Postgres 7.3.4 on RH Linux 7.2. > > > > I need a query to get the Childs of a parent (Top down analysis). Need to > > list all the departments(Childs) of a parent organization. The table > > structure is > > Two places to look for examples: > 1. the contrib/tablefunc folder has an example of this sort of thing > 2. search the mailing list articles for CONNECT BY (oracle's name for this > sort of thing) or "tree" > > -- > Richard Huxton > Archonet Ltd > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly