Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Select based on record number in result set

Author  Topic 

thangavelu_arun@yahoo.com
Starting Member

8 Posts

Posted - 2011-09-01 : 12:03:28
Hi,

Thanks for you input.

Lets assume that i have a temp table with 5000 rows, i have to select 100 rows as sample based on the following formula.

x + (i * y) where i goes from 1 to 99

For example, x = 25, y = 50, i from 1 to 99 and first row selected is row number 25. Here x and y are generated based on the random number and total number of rows taken for selection.

1st row selected - Row number 25 (assume)
2nd row selected - x + (i * y) = 25 + (1 * 50) = 75 Row number
3rd row selected - x + (i * y) = 25 + (2 * 50) = 125 Row number
4th row selected - x + (i * y) = 25 + (3 * 50) = 175 Row number
................
100th row selected - x + (i * y) = 25 + (99 * 50) = 4975 Row number

Is there anyway to include this in a select statement like using mod function?

select * from @table where id = ....


Thanks,
Velu

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-02 : 00:31:34
will you be inputting x and y random values or are they also to be generated as part of the logic?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

thangavelu_arun@yahoo.com
Starting Member

8 Posts

Posted - 2011-09-02 : 08:07:54
They are generated in the logic using random number and number of records in the temp table.
X = total number of records / 100
Y = random number * X

Thanks,
Velu
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-09-02 : 09:19:58
Can you not get away with just selecting a completely random sample?

SELECT TOP 100 *
FROM tbSample
ORDER BY NEWID()


It would be easier. Otherwise you are going to have to give each row a row number based on some sort of ordering.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -