select *
from (select a.CheckDate, b.ProductID, b.Count, b.Price
from tpurchase a, tpurchasedetail b
where a.`Status` in (3, 4)
and a.CheckDate is not null
and a.PurchaseID = b.PurchaseID
order by b.ProductID, a.CheckDate2 desc) m
where (select count(1)
from (select a.CheckDate, b.ProductID, b.Count, b.Price
from tpurchase a, tpurchasedetail b
where a.`Status` in (3, 4)
and a.CheckDate is not null
and a.PurchaseID = b.PurchaseID
order by b.ProductID, a.CheckDate2 desc) s
where m.ProductID = s.productid and m.CheckDate > s.CheckDate) < 1
from (select a.CheckDate, b.ProductID, b.Count, b.Price
from tpurchase a, tpurchasedetail b
where a.`Status` in (3, 4)
and a.CheckDate is not null
and a.PurchaseID = b.PurchaseID
order by b.ProductID, a.CheckDate2 desc) m
where (select count(1)
from (select a.CheckDate, b.ProductID, b.Count, b.Price
from tpurchase a, tpurchasedetail b
where a.`Status` in (3, 4)
and a.CheckDate is not null
and a.PurchaseID = b.PurchaseID
order by b.ProductID, a.CheckDate2 desc) s
where m.ProductID = s.productid and m.CheckDate > s.CheckDate) < 1