sqlserver巧用row_number和partition by分组取top数据

sqlserver巧用row_number和partition by分组取top数据_第1张图片

 

 

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

 

你可能感兴趣的:(sqlserver巧用row_number和partition by分组取top数据)