sql 每个分类取最新的几条数据

1.

select * from(
 select row_number() over(partition by Product_PromotionId order by AutoId desc)as rowindex,* 
 from dbo.Order_Product_Promotion_Details) a
where rowindex<6


2.

select *
from dbo.Order_Product_Promotion_Details t
where AutoId in(select top 5 AutoId from dbo.Order_Product_Promotion_Details 
    where Product_PromotionId=t.Product_PromotionId AND State=2 AND BeginDate<GETDATE() AND EndDate>GETDATE() 
    order by AutoId desc)

 

你可能感兴趣的:(sql)