2

我在用

Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (X64) Jul 9 2008 14:17:44 版权所有 (c) 1988-2008 Microsoft Corporation Enterprise Edition (64-bit) o​​n Windows NT 6.1 (Build 7601: Service Pack 1)

当我尝试按日期排序时,它没有给出正确的顺序

DECLARE @dd table (ID int,rate numeric(5,2), orderdate smalldatetime)
INSERT INTO @dd
SELECT 1,10,'2013-03-05 10:11:00'
UNION ALL 
SELECT 2,25,'2013-03-05 10:11:00' 
UNION ALL
SELECT 3,30,'2013-03-05 10:10:00'
UNION ALL
SELECT 4,50,'2013-03-05 10:11:00'
UNION ALL
SELECT 5,60,'2013-03-05 10:15:00'

SELECT top 2 * FROM @dd Order by orderdate desc

ID 1,2,4 具有相同的orderdate

它以 id 5 和 2 的顺序显示结果,这是不正确的。它实际上应该显示 orderid 5,4。

4

1 回答 1

4

针对您的评论,您还需要按ID列排序:

SELECT top 2 * 
FROM @dd 
Order by orderdate desc, ID desc

结果:

ID  rate    orderdate
5   60.00   2013-03-05 10:15:00
4   50.00   2013-03-05 10:11:00
于 2013-03-05T07:11:16.900 回答