I have a log table as follows:
id status1 status2 ref dateofchange
1 10 12 33 1.1.10
2 10 12 34 1.1.15
3 5 8 14 1.5.10
4 10 12 33 2.1.10
and another table tab as follows:
id ref qty commitdate
1 17 5 1.1.10
2 33 8 1.1.10
3 34 7 1.12.14
4 34 8 1.2.16
5 34 8 1.1.15
I have a query which gives me rows from log table:
select *
from log
where status1=10 and status2=12
this gives:
id status1 status2 ref dateofchange
1 10 12 33 1.1.10
2 10 12 34 1.1.15
4 10 12 33 2.1.10
For each of these rows I want to delete all rows from tab where log.ref=tab.ref and tab.commitdate<=log.dateofchange
after deletion tab table should look like:
id ref qty commitdate
1 17 5 1.1.10
4 34 8 1.2.16
i tried to do it with WITH query:
With l as (
select *
from log
where status1=10 and status2=12
)
delete from tab where l.ref=tab.ref and tab.commitdate<=l.dateofchange
However this does not work.
ERROR: missing FROM-clause entry for table "l"
How can I do that?