日期:2014-05-18 浏览次数:20789 次
-->生成测试数据 declare @tb table([id] int,[datetime1] Datetime,[number] int) Insert @tb select 1,'2008-1-1',5 union all select 2,'2008-1-2',5 union all select 3,'2008-1-3',8 union all select 4,'2008-1-4',7 union all select 5,'2008-1-5',7 Select top 2 max([id]),max([datetime1]),[number] from @tb group by [number] order by 2 desc /* (5 row(s) affected) number ----------- ----------------------- ----------- 5 2008-01-05 00:00:00.000 7 3 2008-01-03 00:00:00.000 8 (2 row(s) affected) */
------解决方案--------------------
Select top 2 max([id]) as [id] ,convert(nvarchar(10),max([datetime1]),120) as [datetime1],[number] from @tb group by [number] order by 2 desc /* id datetime1 number ----------- ---------- ----------- 5 2008-01-05 7 3 2008-01-03 8 (2 row(s) affected) */
------解决方案--------------------
select top 2 * from 表名 where id in (select max(id) from 表名 group by number) order by [datetime1] desc