select * from
(
select *, ROW_NUMBER() over(partition by accountid order by opentime DESC) as rowNum
from ep_productopenhistory
WHERE accountid IN
(
SELECT accountid FROM dbo.ep_accountno
WHERE name IN
(
'1334760',
'1334761',
'1334762',
'1334763',
'1334764',
'1334765',
'1334766',
'1334767'
)
)
) ranked
where ranked.rowNum =1
order by ranked.accountid, ranked.opentime desc