1

Does anyone have a query that will select n number of random rows from a SQL Server CE 4 table?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ipr101
  • 24,096
  • 8
  • 59
  • 61

1 Answers1

3

Have you tried:

SELECT TOP N * FROM table ORDER BY NEWID()

NEWID causes a GUID (semi-random identifier) to be created for each row in the result set and sorting by it ought to give you N fairly random results.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • I thought I'd tried `NEWID()` before I asked the question and it didn't work, so I presumed it wasn't available with CE. I've just tried it again though and it works perfectly, so I was obviously making a silly mistake elsewhere. Thanks for the help! – ipr101 Feb 12 '12 at 21:07