First off, you shouldn't use HAVING the way you are. HAVING should only be used for aggregated columns. It will work for non-aggregated rows but that's not the way to do it. The secret is this: WHERE filters records before aggregation. HAVING does it after. Yes, the query plan may be the same for simple examples like this but you might as well develop good habits from the start!SELECT DISTINCT TOP (100) PERCENT ID, TO_LOC, MAX(DISTINCT CountNum) AS CountNumFROM DatabaseWHERE(ID = '1890433')GROUP BY ID, TO_LOC
This way, you can be sure that SQL will only be doing the MAX calculation for three rows you're interested in. Now, I think you want only the row with the highest CountNum. This query will do it efficiently:SELECT DISTINCT TOP (1) ID, TO_LOC, MAX(DISTINCT CountNum) AS CountNumFROM DatabaseWHERE (ID = '1890433')GROUP BY ID, TO_LOCORDER BY MAX(DISTINCT CountNum) desc