select response.id,
response.id_type,
response.hierparent,
response.hierparent_type,
response.rkkid,
response.authorbeard,
map.notesunid
from
(select res.id,
res.id_type,
res.hierparent,
res.hierparent_type,
beard.orig_shortname as authorbeard,
res.hierroot as rkkid
from f_dp_resolution res
join f_dp_resltnbase resbase on res.id = resbase.id
join so_beard beard on resbase.author = beard.id
and resbase.author_type = beard.id_type
union
select report.id,
report.id_type,
report.hierparent,
report.hierparent_type,
beard.orig_shortname as authorbeard,
report.hierroot as rkkid
from f_dp_report report
join so_beard beard on
report.author = beard.id
and report.author_type = beard.id_type
) response
join (select SUBSTRING(npm.punid for 16) as sochiunid,
npm.nunid as notesunid
from nunid2punid_map npm) map on
to_char(cast((response.id_type * 10 ^12) as bigint) + response.id, 'FM0000000000000000') = map.sochiunid
where
response.rkkid =(select cmj_document.id
from {table name} cmj_document
join (select SUBSTRING(npm.punid for 16) as sochiunid,
npm.nunid as notesunid
from nunid2punid_map npm) map on
to_char(cast((cmj_document.id_type * 10 ^12) as bigint) + cmj_document.id, 'FM0000000000000000') = map.sochiunid
where map.notesunid = '{cmjunid}') |