SELECT TOP #JobObjectsN#
 Objects_tbl.*,
 Ir.ObjectPuid AS IrPuid,
 Ir.ObjectName AS IrName
FROM
 dbo.Objects_tbl WITH (nolock)
 LEFT OUTER JOIN dbo.Objects_Ir_Rel_tbl WITH (nolock) ON (dbo.Objects_tbl.Id = dbo.Objects_Ir_Rel_tbl.ObjectId)
 LEFT OUTER JOIN dbo.Objects_tbl Ir WITH (nolock) ON (dbo.Objects_Ir_Rel_tbl.IrId = Ir.Id)
WHERE
 Objects_tbl.ObjectClass='DS'
 -- see if Rf (ProcStateCol20) ended with OK or or WRN
 AND (Objects_tbl.ProcStateCol20 like 'D.%OK' or Objects_tbl.ProcStateCol20 like 'D.%WRN')
 AND Objects_tbl.ProcStateCol15 = 'N'
--  AND Objects_tbl.ProcStateCol15 = 'RUN.K1'
 AND Objects_tbl.ObjectOwningSiteID='#TcSiteId#'
--  AND Objects_tbl.ObjectUgMaster_Type = 'AP'
--  AND Objects_tbl.ObjectUgMaster_Type = 'SP'
 AND Objects_tbl.ObjectIrLatestRank<=-2
ORDER BY
 Objects_tbl.ObjectUgMaster_APLevels DESC,
 Objects_tbl.ObjectUgMaster_CompCount DESC,
 Objects_tbl.ObjectIrLatestRank ASC
