SELECT c.sMaterialNo,
c.sMaterialName,
c.nACPrice,
c.nTaxRate,
c.tStoreInTime
FROM
(
SELECT b.sMaterialNo,
b.sMaterialName,
b.nACPrice,
b.nTaxRate,
a.tStoreInTime,
ROW_NUMBER() OVER (PARTITION BY b.sMaterialNo ORDER BY a.tStoreInTime DESC) AS new_index
FROM dbo.mmYNInHdr a
JOIN dbo.mmYNInDtl b
ON a.uGUID = b.ummInHdrGUID
WHERE nACPrice > 0
) c
WHERE c.new_index = 1
and (/*Condition*/ )
ORDER BY c.tStoreInTime DESC;