WITH repeated AS
- ( select nn.node2_id AS id, count(*) AS c
- FROM nodes_nodes nn
- GROUP BY nn.node2_id
+ (
+ select nn.context_id AS id, count(*) AS c
+ FROM nodes_contexts nn
+ GROUP BY nn.context_id
)
-DELETE FROM nodes n
+DELETE FROM contexts c
USING repeated r
WHERE
-n.id = r.id
-AND r.c <= 1
-AND n.typename = 4
+c.id = r.id
+AND r.c = 1
+AND c.typename = 4
;