I originally had written the following
SELECT t1.TransactionNumber
FROM t1
JOIN
(
SELECT MAX(id) id
FROM t1
WHERE Period BETWEEN '01-11-2013' and '01-12-2014'
GROUP BY AccountNumber
) t2
on t1.id= t2.id
But it was too slow. It took around 20 seconds, so as a test, I changed it to the following
SELECT MAX(id) AS id
INTO #t2
FROM t1
WHERE Period BETWEEN '01-11-2013' and '01-12-2014'
GROUP BY AccountNumber
SELECT t1.id
FROM t1
JOIN #t2 t2
ON t1.id= t2.id
The second query took only 1 second to run. The second query does a index seek using the PK key, whereas the first key does a scan.
Note: id is the primary key clustered on the t1 table.