I haven't done this in MSSQL in a while so please forgive me if my syntax is incorrect but you could use a subquery:
select distinct [TrackingID],
max(InternalID)
from MyDataBase
where [TrackingID] in
(select DISTINCT [TrackingID] as tracking
FROM [MyDataBase]
Where [InternalID] <=45 AND [InternalID] >= 20)
group by [TrackingID]
sqlfiddle link:http://sqlfiddle.com/#!6/61e176/6
I has to use the max()
aggregate function to allow for the group by
which gives me exactly the result set I was trying to get. Your mileage may vary as it seems that you are possibly getting back dirty data if you have "far far too many" "unique" ids being returned by your query. For a more exactly solution, please provide part of your data or result set.