Where did you add the WHERE clause? Take a look at the example that I constructed to see if that gives you the correct results. If not, what is the output that you would expect?CREATE TABLE #tmp(fileId INT, submitDate DATETIME, STATUS VARCHAR(32));INSERT INTO #tmp VALUES (1,'20120101','booked');INSERT INTO #tmp VALUES (1,'20120102','available');INSERT INTO #tmp VALUES (2,'20120101','available');INSERT INTO #tmp VALUES (2,'20120102','booked');SELECT COUNT(*)FROM #tmp a INNER JOIN( SELECT fileId, MAX(submitdate) AS MaxDate FROM #tmp GROUP BY fileId) b ON a.fileId = b.fileId AND a.submitDate = b.MaxDateWHERE a.status = 'booked'; DROP TABLE #tmp;