Connecting Tech Pros Worldwide Forums | Help | Site Map

Select every 79th record

ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#1: May 15 '09
I have 400K records on my table. Now I wanted you pull out random records that falls every 79th records from top to bottom. How can you do that on sql?

Your help is much appreciated.
thanks,
DM

prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#2: May 16 '09

re: Select every 79th record


yes .. try this

$sql = "select * from table_name order by RAND() limit 79 "
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#3: May 16 '09

re: Select every 79th record


just a clarification i wanted to select the records on my table that has 437K row any records that falls on every 79th records. To sum up i should get aroun 5,500 number of records filtering it from top to bottom.

im using ms sql server and i had error

SQL
Quote:
select * FROM employee with(nolock) order by RAND() limit 79
Error Msg
Expand|Select|Wrap|Line Numbers
  1. Msg 102, Level 15, State 1, Line 1
  2. Incorrect syntax near 'limit'.
  3.  
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#4: May 16 '09

re: Select every 79th record


try with out "with (NOLOCK)"

and that gonna wrk

Quote:
select * FROM employee order by RAND() limit 79
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#5: May 16 '09

re: Select every 79th record


Moved to SQL Server forum.
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#6: May 18 '09

re: Select every 79th record


prabirchoudhury, it's still not working same error of
Quote:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'limit'.
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#7: May 18 '09

re: Select every 79th record


Use ROW_NUMBER() function as inside a subquery and put a WHERE on the outer query where RownumberReturned mod 79 = 0

That's a pseudo-code.

Happy Coding!

--- CK
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#8: Sep 8 '09

re: Select every 79th record


Quote:

Originally Posted by ck9663 View Post

Use ROW_NUMBER() function as inside a subquery and put a WHERE on the outer query where RownumberReturned mod 79 = 0

That's a pseudo-code.

Happy Coding!

--- CK

im having syntax error on below sql query
Quote:
Msg 207, Level 16, State 1, Line 4
Invalid column name 'RowNumber'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'RowNumber'.
Expand|Select|Wrap|Line Numbers
  1. SELECT *,
  2.     ROW_NUMBER() OVER (ORDER BY RAND()) AS 'RowNumber'
  3.     FROM users with (nolock)
  4.     where RowNumber between  79 and 0
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#9: Sep 10 '09

re: Select every 79th record


Use it as a subquery instead.

Happy Coding!!!

--- CK
Reply


Similar Microsoft SQL Server bytes