TEXT TO lcSelect NOSHOW TEXTMERGE PRETEXT 15 SELECT * FROM Table1 a LEFT JOIN (SELECT *, RANK() OVER (PARTITION BY unitcode, usertyped ORDER BY TimeStamp DESC) AS Test FROM Table2) b ON LTRIM(RTRIM(a.unitcode)) = LTRIM(RTRIM(b.unitcode)) AND LTRIM(RTRIM(a.unittype)) = LTRIM(RTRIM(b.usertyped)) AND b.Test < 6 WHERE a.inactive = 'TRUE' -- and EXISTS ( SELECT * FROM Table1 WHERE inactive = 'TRUE' ) this is extra, if there are -- no records with inactive = 'TRUE' this query didn't returns any records. ORDER BY a.unitcode, a.unittype, b.timestamp DESC ENDTEXT>Thanks Borislav, I'll play around with it today if I can...