 Select
      i.puid               AS ItemPuid,
      i.pitem_id           AS ItemId,
      ir.puid              AS IrPuid,
      ir.pitem_revision_id AS IrId,
      '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
      i.puid               AS ItemPuid,
      i.pitem_id           AS ItemId,
      ir.puid              AS IrPuid,
      ir.pitem_revision_id AS IrId,
      'SequNo' As Source
    From
      infodba.PITEMREVISION ir
    Left Join infodba.PITEM I
    On
      ir.RITEMS_TAGU = I.PUID
    Where
      ir.RSEQUENCE_ANCHORU Is Null
