98 lines
2.3 KiB
PL/PgSQL
98 lines
2.3 KiB
PL/PgSQL
CREATE OR REPLACE FUNCTION filter3() RETURNS void AS $$
|
|
DECLARE
|
|
filler RECORD;
|
|
i int4;
|
|
BEGIN
|
|
|
|
--CREATE TABLE xc (id int4, hid text, wid text, value int4) WITHOUT OIDS;
|
|
--i:=0;
|
|
|
|
LOOP
|
|
|
|
SELECT INTO filler
|
|
_f1.cid as cleft, _f2.cid as cright, _f1.hid as hleft, _f2.hid as hright
|
|
FROM f3 _f1, f3 _f2
|
|
WHERE _f1.tid = _f2.tid and _f1.Col IN ('POWSTATE','POB') and
|
|
_f2.Col IN ('POWSTATE','POB') and _f1.cid > _f2.cid;
|
|
|
|
|
|
IF NOT FOUND THEN EXIT; END IF;
|
|
|
|
EXECUTE '
|
|
create table k1 without oids as
|
|
select _c.* from c3 _c, f3 _f
|
|
where ' || quote_literal(filler.cleft) || ' = _f.cid and _f.hid = _c.hid;
|
|
';
|
|
|
|
EXECUTE '
|
|
create table k2 without oids as
|
|
select _c.* from c3 _c, f3 _f
|
|
where ' || quote_literal(filler.cright) || ' = _f.cid and _f.hid = _c.hid;
|
|
';
|
|
|
|
EXECUTE '
|
|
update f3
|
|
set cid = ' || quote_literal(filler.cleft) || '|| \'x\' || ' || quote_literal(filler.cright) || '
|
|
where cid =' || quote_literal(filler.cleft) || ' or cid=' || quote_literal(filler.cright) || ';
|
|
|
|
create table ctemp3 as
|
|
select distinct a.hid, a.wid || \'x\' || b.wid as wid, a.value
|
|
from k1 a, k2 b
|
|
union
|
|
select distinct b.hid, a.wid || \'x\' || b.wid as wid, b.value
|
|
from k1 a, k2 b;
|
|
|
|
delete from c3 where
|
|
hid in (select hid from k1) or hid in (select hid from k2);
|
|
';
|
|
|
|
EXECUTE '
|
|
create table toinsert3 as
|
|
select _c1.value as value, _c1.hid as hid, _c1.wid as wid, _f2.hid as fhid
|
|
from ctemp3 _c1, f3 _f1, f3 _f2
|
|
where _f1.tid = _f2.tid and _f1.hid = _c1.hid and _f1.cid = _f2.cid and
|
|
_f1.Col in (\'POWSTATE\',\'POB\')
|
|
and _f2.Col in (\'POWSTATE\',\'POB\');
|
|
';
|
|
|
|
EXECUTE '
|
|
insert into c3
|
|
select * from ctemp3
|
|
where value in (
|
|
select value from toinsert3
|
|
where toinsert3.wid = ctemp3.wid and toinsert3.fhid = ctemp3.hid
|
|
and toinsert3.hid <> ctemp3.hid);
|
|
';
|
|
|
|
|
|
--EXECUTE '
|
|
--insert into c3
|
|
--select * from ctemp3
|
|
--where value in (
|
|
--select _c1.value from ctemp3 _c1, f3 _f1, f3 _f2
|
|
--where _c1.hid <> ctemp3.hid and _f1.tid = _f2.tid and
|
|
-- _f1.hid = _c1.hid and _f2.hid = ctemp3.hid and
|
|
-- _c1.wid = ctemp3.wid and _f1.cid = _f2.cid and
|
|
-- _f1.Col in (\'POWSTATE\',\'POB\')
|
|
-- and _f2.Col in (\'POWSTATE\',\'POB\'));
|
|
--';
|
|
|
|
|
|
--EXECUTE '
|
|
--insert into xc
|
|
--SELECT '|| quote_literal(i) || ', * FROM c3;';
|
|
|
|
EXECUTE '
|
|
drop table k1;
|
|
drop table k2;
|
|
drop table ctemp3;
|
|
drop table toinsert3;
|
|
';
|
|
|
|
--i := i+1;
|
|
|
|
END LOOP;
|
|
RETURN;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|