Please try order by ID
instead of order by PhotoName
:
;with cte as
(
select
ADID,
PhotoName,
PhotoPath,
rank() over(partition by AdID order by ID) as rn
from AdPhoto
)
select ADID,
PhotoName,
PhotoPath
from cte
where rn = 1