Hello,
I don't understand what is wrong with following query, when I search on the net I find simular sql statements .... my cte works (tested seperately), I've used the for loop just to copy from one table to another ... but when I combine them it goes wrong...
The code that is in commet are some variations I have tried.
with x as (
select
p.id as id, p.naam as pers , c.id as comp, cp.sectorid as sect
from competentiepersoon cp
join persoon p on p.id = cp.persoonid
join competentie c on c.id = cp.competentieid
where sectorid = 14 --c1.id
),
y as (
select t1.comp as comp1, t2.comp as comp2, t1.sect, count(*) as tel
from x t1, x t2
where t1.comp != t2.comp
and t1.id = t2.id
group by t1.comp, t2.comp, t1.sect
),
z as (
select
comp1, comp2, sect, tel / (select count(*) from y where comp1 = y1.comp1 or comp2 = y1.comp1 or comp1 = y1.comp2 or comp2=y1.comp2 ) as match
from y y1
)
--insert INTO competentiematching(competentieid1, competentieid2, sectorid, matching)
--values select comp1, comp2, sect, match from z;
for toInsert in ( select * from z )
loop
insert into competentiematching values(toInsert.comp1, toInsert.comp2,toInsert.sect,toInsert.match);
end loop;
=> keeps giving "missing select statement"
ps sorry for my english :).