1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | --1.创建测试表 Create Table #Order1 ( OrderName varchar (50), RequestDate datetime, OrderCount int ) -- 插入测试数据 insert into #Order1 values ( '衣服' , '2013-06-02' ,2) insert into #Order1 values ( '衣服' , '2013-06-03' ,4) insert into #Order1 values ( '衣服' , '2013-06-04' ,5) insert into #Order1 values ( '鞋' , '2013-06-02' ,3) insert into #Order1 values ( '鞋' , '2013-06-03' ,7) insert into #Order1 values ( '鞋' , '2013-06-04' ,8) --选取每组中时间最近的一条 -- 方法1 select * from ( select ROW_NUMBER()over(PARTITION By OrderName ORDER BY RequestDate DESC ) as rownum, OrderCount,OrderName from #Order1 ) T where T.rownum = 1 --group by OrderName drop table #Order1 |