SELECT * FROM( SELECT orderid,createtime, ROW_NUMBER() over(PARTITION by orderid order by createtime desc) num FROM dbo.yborder_ordernotes WHERE orderid IN ( SELECT id FROM yborder_order WHERE state =10 ) ) tbl WHERE tbl.num=1