I have the following query which uses multiple CTE's:
;with Test as (select plan_id, max("id") maxid
from Scheme
group by plan_id),
Rec as (select rh.* from Scheme rh, Test
where rh.plan_id = Test.plan_id
and rh.id = Test.maxid
and rh.event_id = 20)
delete from Scheme rh USING Rec
where rh.id = "Rec".id;
I am getting following error :
[Err] ERROR: missing FROM-clause entry for table "Rec"
LINE 9: where rh.id = "Rec".id;
If I re-write the query using just one CTE, then the delete from "table" USING "CTE" syntax works. What is it that I am missing in the above query?