/* TCUA:IrId exist? 06.04.13 / Up:06.12.13 */
Select  IrRev from ( 
Select
      ir.pitem_revision_id AS IrRev,
      ir.puid              AS IrPuid
     -- ,'SequYes' As Source
    From
      infodba.PITEMREVISION ir
    Inner Join
      (Select ir.ritems_tagu || ir.rsequence_anchoru Id, Max(ir.psequence_id) MaxSeq
        From  infodba.PITEMREVISION ir
        Where ir.rsequence_anchoru Is Not Null
        Group By ir.ritems_tagu || ir.rsequence_anchoru
      ) HSeq
    On ir.ritems_tagu || ir.rsequence_anchoru = HSeq.Id
       and ir.psequence_id = HSeq.MaxSeq
    Left Join infodba.PITEM i On (ir.ritems_tagu = i.puid)
    Union All (
     Select
      ir.pitem_revision_id AS IrRev, 
      ir.puid              AS IrPuid
      -- ,'SequNo' As Source
    From
      infodba.PITEMREVISION ir
    Left Join infodba.PITEM i On (ir.ritems_tagu = i.puid)
    Where ir.rsequence_anchoru Is Null)
) ItemRev
where IrPuid = :ir_puid