/* TCE BOM ImPrecice: redrives BOMLines latest.cr Rev L.Up 11.08.17 */
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,
  bo.pqty_value            AS BOML_qty_val,  
  bo.pext_transform_rot00  AS BOML_trf_rot00,
  bo.pext_transform_rot01  AS BOML_trf_rot01,
  bo.pext_transform_rot02  AS BOML_trf_rot02,
  bo.pext_transform_rot10  AS BOML_trf_rot10,  
  bo.pext_transform_rot11  AS BOML_trf_rot11,
  bo.pext_transform_rot12  AS BOML_trf_rot12,
  bo.pext_transform_rot20  AS BOML_trf_rot20,
  bo.pext_transform_rot21  AS BOML_trf_rot21, 
  bo.pext_transform_rot22  AS BOML_trf_rot22,
  bo.pext_transform_per0   AS BOML_trf_per0,
  bo.pext_transform_per1   AS BOML_trf_per1,
  bo.pext_transform_per2   AS BOML_trf_per2,
  bo.pext_transform_tra0   AS BOML_trf_tra0,
  bo.pext_transform_tra1   AS BOML_trf_tra1,
  bo.pext_transform_tra2   AS BOML_trf_tra2,
  bo.pext_transform_invscale AS BOML_trf_invscale,  
  bo.pref_designator      AS BOML_ref_designator,
  bo.rchild_itemu         AS BOML_child_itemu,
  bo.rchild_itemc         AS BOML_child_itemc,
  bo.poccurrence_name     AS BOML_occurre_name,
  bo.poccurrence_type     AS BOML_occurre_type,
  bo.porder_no            AS BOML_order_no,
  bvr.pstruct_last_mod_date AS BOMV_pstruct_last_mod_date,
  bvr.puid AS BOMV_puid,
  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.PPOM_APPLICATION_OBJECT paoIr ON (ir.puid = paoIr.puid)
WHERE
     ir2.pitem_revision_id = :para_ir_pitem_revision_id
 AND i2.pitem_id = :para_i_pitem_id
 AND upper(bvt.pname) in ('VIEW', 'CATIA_MULTI_MODEL', 'CATIA')
 ) t
where rnk = 1

