2013-08-28 39 views
0

它是由compcobalt通过这个线程问(用于SQL Server ):Select random rows and stop when a specific sum/total is reached选择随机行和停止在特定金额/总达到

详细信息:


我使用SQL Server 2012,我试图做这样的事情:

SELECT SUM(MILES) from tblName WHERE 
mDate > = '03/01/2012' and 
mDate <= '03/31/2012' 
-- and... 
/* 
    now I want to add here do until the SUM of Miles 
    is equal to or greater then '3250' and get the 
    results rows randomly 
*/ 

换句话说,我想从已经指定由和日期表中选择任意行和停止时的里程总和达到或超过数:3250


但我的问题是如何与SQL Server做?谢谢

回答

0

这是一个棘手的问题,可以完成。我的想法是 1.给你的表格添加行号。 2.然后生成随机数字。 3.借助row_number和随机生成的编号选择行。 4.继续做下去,直到和你一样。

但问题是SQL Server 2000中没有从http://forums.asp.net/t/1630106.aspx/1

ROW_NUMBER函数 采取帮助生成随机数PLZ看到

generate x digit random number in sql server 2008 R2

好运

请更新我,如果我有任何帮助

Regar ds

Ashutosh Arya

+0

因此,没有纯粹的sql查询做到这一点,对吧? –