日期:2014-05-18  浏览次数:20565 次

关于sqlserver中ROW_NUMBER()中OVER里Order By字段的问题
如果用ROW_NUMBER() OVER (ORDER BY Column ASC)会产生这样一个怪问题,
那就是当Column有重复数据时,同一条sql语句每次查询出来的数据会不一样,比如Column是date类型,我有一万条数据的这个字段的值是2012-05-30,那么我查询其中10条数据时,每次执行结果都不一样,这个问题应该怎么解决,并且对性能影响不大。

SQL code

;WITH ViewList ( [row], PartNumber, SerialNumber,EndUserName,PurchaseDate,DealerName,EndUserProvince,EndUserCity,EndUserAddress,LotNumber,DeliveryDate,ReceiptDate) 
AS (SELECT ROW_NUMBER() OVER (ORDER BY  ReceiptDate ASC) [row],PartNumber, SerialNumber,EndUserName,PurchaseDate,DealerName,EndUserProvince,EndUserCity,
EndUserAddress,LotNumber,DeliveryDate,ReceiptDate FROM [dbo].[ScannerWarranty]) SELECT [row], PartNumber, SerialNumber,EndUserName,PurchaseDate,DealerName,
EndUserProvince,EndUserCity,EndUserAddress,LotNumber,DeliveryDate,ReceiptDate FROM ViewList WHERE [row] BETWEEN  91 AND 100



每次执行结果都不一样,蛋疼了……

------解决方案--------------------
那就这样 ROW_NUMBER() OVER(ORDER BY GETDATE()) RN
------解决方案--------------------
SQL code
;WITH ViewList ( [row], PartNumber, SerialNumber,EndUserName,PurchaseDate,DealerName,EndUserProvince,EndUserCity,EndUserAddress,LotNumber,DeliveryDate,ReceiptDate) 
AS (SELECT ROW_NUMBER() OVER (ORDER BY  GETDATE() ) [row],PartNumber, SerialNumber,EndUserName,PurchaseDate,DealerName,EndUserProvince,EndUserCity,
EndUserAddress,LotNumber,DeliveryDate,ReceiptDate FROM [dbo].[ScannerWarranty]) SELECT [row], PartNumber, SerialNumber,EndUserName,PurchaseDate,DealerName,
EndUserProvince,EndUserCity,EndUserAddress,LotNumber,DeliveryDate,ReceiptDate FROM ViewList WHERE [row] BETWEEN  91 AND 100

------解决方案--------------------
SQL code
ORDER BY  ReceiptDate,GETDATE()

------解决方案--------------------
探讨
楼上的几位,先谢了,可是我是点击某一个列就按这个列排序的,如果加个Getdate(),那不是会影响查询速度