SQL Query for Top Down fetching of childs

From: "Kumar" <sgnerd(at)yahoo(dot)com(dot)sg>
To: "psql" <pgsql-sql(at)postgresql(dot)org>
Subject: SQL Query for Top Down fetching of childs
Date: 2004-01-29 06:11:18
Message-ID: 00ad01c3e62e$b70ccf60$7502a8c0@hdsc.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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;

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

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)

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)

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;

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
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
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;

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message jodi 2004-01-29 08:27:59 auto_insert
Previous Message Octavio Alvarez 2004-01-29 04:27:00 LEFT JOIN on one and/or another column