pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kumar <sgnerd@yahoo.com.sg>
To: psql <pgsql-sql@postgresql.org>
Subject: SQL Query for Top Down fetching of childs
Date: Thu, 29 Jan 2004 11:41:18 +0530
Message-ID: <00ad01c3e62e$b70ccf60$7502a8c0@hdsc.com> (raw)

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

view thread (3+ messages)  latest in thread

Message-ID: <00ad01c3e62e$b70ccf60$7502a8c0@hdsc.com>
Permalink:  ../00ad01c3e62e$b70ccf60$7502a8c0@hdsc.com/
Also on:    postgresql.org/message-id/00ad01c3e62e$b70ccf60$7502a8c0@hdsc.com

 · 

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: sgnerd@yahoo.com.sg
  Subject: Re: SQL Query for Top Down fetching of childs
  In-Reply-To: <00ad01c3e62e$b70ccf60$7502a8c0@hdsc.com>

* 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