/* TCUA:Ir exist? 06.04.13 / Up:13.05.13 */
SELECT ir.pitem_revision_id As IrRev
FROM   infodba.PITEMREVISION ir
WHERE 
   (ir.puid=:ir_puid AND ir.rsequence_anchoru IS Null)
OR (ir.puid=:ir_puid AND ir.rsequence_anchoru = 'AAAAAAAAAAAAAA')   
OR (ir.puid=:ir_puid AND ir.psequence_id =
 (SELECT DISTINCT MAX(ir2.psequence_id)
  FROM infodba.PITEMREVISION ir
       INNER JOIN infodba.PITEMREVISION ir2
       ON (     ir.rsequence_anchoru = ir2.rsequence_anchoru
            AND ir.ritems_tagu = ir2.ritems_tagu
          )
  WHERE ir.puid=:ir_puid))