I take it it doesn't matter that Juan Silva has 25 visits at another
address, and John Doe has 200, 50, and 220 visits at 3 addresses,
which adds up to more than Juan Silva's 225 + 25.
So - I'm not on the server just now, but, will this work?
SELECT
CUSTOMER.ID,
CUSTOMER.NAME, CUSTOMER.ADDR, CUSTOMER.CNT
FROM
@T CUSTOMER
JOIN
(
SELECT ID, MAX(CNT) AS MAX_VISITS FROM @T GROUP BY ID
)
MAX_VISITS
ON
(
CUSTOMER.ID =
MAX_VISITS.ID )
ORDER BY
MAX_VISITS.MAX_VISITS DESC
,
CUSTOMER.CNT DESC
-- I think if you /do/ want to give John Doe a count of 470 visits
-- for ranking, then you just need to use SUM(CNT) instead of MAX(CNT).
-- And probably change MAX_VISITS to ALL_VISITS or something.
--
-- And indexes... well, a non-unique index on ID, anyway.
-- Or a unique index on (ID, ADDR), but that's bad. Is there
-- an ADDR_ID column that you could pick up? And if you put CNT in
-- there too, it looks crazy and it makes updates slower but
-- I think it helps this query. Or, just create the table of
-- MAX_VISITS.
--
-- In recent editions of SQL Server, you can also pre-define sub-queries
-- by starting a statement with the keyword "WITH"; confusingly,
-- WITH is also used to introduce "hints", so, make sure you get
-- the /right/ page in the manual. It's late here and I don't remember
-- how it goes, sorry ;-)