Unix Technical Forum

Re: recursive processing

This is a discussion on Re: recursive processing within the pgsql Novice forums, part of the PostgreSQL category; --> Hi Keith, This function accomplishes what you want (at least in my testing), but I'm no expert on PL/pgSQL ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 09:30 PM
Doug Gorley
 
Posts: n/a
Default Re: recursive processing

Hi Keith,

This function accomplishes what you want (at least in my testing), but
I'm no expert on PL/pgSQL programming; If anyone has any recommendations
on how to do this better, I'd live to hear it.

Thanks,
Doug Gorley | douggorley (at) shaw (dot) ca

---
---
create or replace function build_assemblies()
returns integer
as $$
declare
compound tbl_assembly%ROWTYPE;
replacement tbl_assembly%ROWTYPE;
begin
for compound in execute 'select
*
from
tbl_assembly
where
component_id in (select item_id from tbl_assembly)'
loop
for replacement in select
*
from
tbl_assembly
where
item_id = compound.component_id
loop
insert into tbl_assembly values
(
compound.item_id,
replacement.component_id,
compound.quantity * replacement.quantity
);
end loop;
delete from
tbl_assembly
where
item_id = compound.item_id
and component_id = compound.component_id;
end loop;
return 1;
end;
$$ language plpgsql;
--
--


> Hi All,
>
> I have two tables. The first table (tbl_item) contains an item id and
> it's type.
>
>tbl_item
>item_id | item_type
>--------+----------
> A | DIR
> B | DIR
> C | ASY
> D | DIR
> E | DIR
> F | DIR
> G | ASY
>
>
> The second table (tbl_assembly) contains the components of the
> assemblies. It is possible that an assembly is made up of any quantity
> of DIR items or one or more assemblies
>
>tbl_assembly
>item_id | component_id | quantity
>--------+--------------+---------
> C | A | 2
> C | B | 4
> G | C | 3
> G | E | 1
> G | F | 8
>
>
> I would like to perform some recursive processing to replace any
> assembly used as a component with the appropriate number of components
> so that all component_ids are of item_type = 'DIR'.
>
>item_id | component_id | quantity
>--------+--------------+---------
> C | A | 2
> C | B | 4
> G | A | 6
> G | B | 12
> G | E | 1
> G | F | 8
>
>
> I want to perform this processing any time an item_id is INSERTed or
> UPDATEDed into tbl_assembly (TRIGGER) and place this result back into
> the assembly table.
>
> Any assistance and URL's to documentation or examples is appreciated.
>
>--
>Kind Regards,
>Keith
>



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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com