/* TCUA BOM ImPrecice: redrives BOMLines latest.cr Rev L.Up 20.01.15 */
SELECT * from
(
SELECT
  i.puid         AS BOML_i_puid,
  i.pitem_id     AS BOML_pitem_id,
  ir.puid        AS BOML_ir_puid,
  ir.pitem_revision_id AS BOML_pitem_revision_id,
  bo.pseq_no     AS BOML_pseq_no,
  bo.puid        AS BOML_puid,
  bo.rnotes_refu AS BOML_OcN_ptt_puid,
  bvr.pstruct_last_mod_date AS BOMV_pstruct_last_mod_date,
  rank() over (partition by i.puid order by paoIr.pcreation_date desc) rnk
FROM
            infodba.PITEM                   i2
 INNER JOIN infodba.PITEMREVISION           ir2 ON (i2.puid = ir2.ritems_tagu)
 INNER JOIN infodba.PSTRUCTURE_REVISIONS    ps ON (ir2.puid = ps.puid)
 INNER JOIN infodba.PPSBOMVIEWREVISION      bvr ON (ps.pvalu_0 = bvr.puid)
 INNER JOIN infodba.PPSOCCURRENCE           bo ON (bvr.puid = bo.rparent_bvru)
 INNER JOIN infodba.PITEM                   i ON (bo.rchild_itemu = i.puid)
 INNER JOIN infodba.PITEMREVISION           ir ON (i.puid = ir.ritems_tagu)
 LEFT JOIN  infodba.PPSBOMVIEW              bv on (bvr.rbom_viewu = bv.puid)
 LEFT JOIN  infodba.PPSVIEWTYPE             bvt on (bvt.puid = bv.rview_typeu)
 LEFT JOIN  infodba.PWORKSPACEOBJECT        pwo On (ir.puid = pwo.puid)
 LEFT JOIN  infodba.PPOM_APPLICATION_OBJECT paoIr ON (ir.puid = paoIr.puid)
WHERE
  (upper(bvt.pname) = 'VIEW' OR
   upper(bvt.pname) = 'CATIA_MULTI_MODEL' OR
   upper(bvt.pname) = 'CATIA')
 AND ir2.pitem_revision_id = :para_ir_pitem_revision_id
 AND i2.pitem_id = :para_i_pitem_id
 AND pwo.pactive_seq = 1
 ) t
where rnk = 1
