View v_documents
stores only the latest flow_id, but each document can have multiple older flows as well.
View v_flow_ids
has two columns - flow_id and parent_id, and using this view it is possible to find all the successive flow_ids for one particular document, starting with the latest flow_id.
However, when doing this for multiple documents at once, I lose track of which flow_id belongs to which document:
select ff.flow_id, max(ff.repo_id) as repo_id --d.id would also be necessary here
from (select f.flow_id
from v_flow_ids f
start with f.flow_id in (select d.flow_id
from v_documents d
where d.id in (28211,14631,28212,14632,28192))
connect by prior f.parent_id = f.flow_id) t
left join v_flow_additional_info ff on t.flow_id = ff.flow_id
group by ff.flow_id, ff.external_id
Is it possible to somehow also get the document id in the final result? So that each flow_id is assigned its corresponding v_documents.id?
JOIN v_documents
to the other queries and use CONNECT_BY_ROOT
, something like:
SELECT ff.flow_id,
max(ff.repo_id) as repo_id,
t.id
FROM (
SELECT f.flow_id,
CONNECT_BY_ROOT(d.id) AS id
FROM v_flow_ids f
LEFT OUTER JOIN v_documents d
ON f.flow_id = d.flow_id
START WITH d.id IN (28211,14631,28212,14632,28192)
CONNECT BY PRIOR f.parent_id = f.flow_id
) t
LEFT OUTER JOIN v_flow_additional_info ff
ON t.flow_id = ff.flow_id
GROUP BY
ff.flow_id,
ff.external_id,
t.id
However, without some sample data and an idea of what your expected output is, it is impossible to test.