Мой CTE работает с бесконечным l oop для счета, и я не могу выяснить, почему.
Последующая версия: EnterpriseDB 10.1.5 для x86_64-p c - linux -gnu, скомпилированный g cc (G CC) 4.4.7 20120313 (Red Hat 4.4.7-18), 64-битный
Вот запрос:
WITH recursive derived AS(
SELECT inv_num, new_inv_num, 0 AS LEVEL
FROM "CBF".cbf_inv_gen_trans o
WHERE o.inv_num = '082003G10012730'
AND o.new_inv_num NOT LIKE '%XX%'
AND o.new_inv_num IS NOT NULL
AND o.attribute_id IS NOT NULL
AND o.attr_val IS NOT NULL
UNION
SELECT e.inv_num, e.new_inv_num, LEVEL + 1
FROM "CBF".cbf_inv_gen_trans e
inner join derived s ON s.new_inv_num = e.inv_num
WHERE e.new_inv_num NOT LIKE '%XX%'
AND e.new_inv_num IS NOT NULL
AND e.attribute_id IS NOT NULL
AND e.attr_val IS NOT null)
SELECT new_inv_num
FROM derived
WHERE LEVEL = (SELECT max(LEVEL) FROM derived) limit 1;