On Apr 1, 2011, at 8:48 PM, Jeremy Evans wrote: > On Apr 1, 3:06 pm, Andy Tinkham <[email protected]> wrote: >> Once I get a sequel dataset built, I'd like to pick one element at random >> from it. What's the best way to do this? Retrieve an array of all the key >> values and pick one randomly and then filter the entire dataset on that >> value? Use the limit function with a limit of 1 and an offset of a random >> number from 0 to the dataset count - 1? Some other way that I'm not thinking >> of? > > There's rarely a database independent way of doing > so. .order{rand{}}.first will work for some databases, I think others > use random instead of rand. And that way in general is slow for large > datasets. If you know the number of records, .limit(1, rand * num) > will also work (as you suggest), but can be slow as well. Fast ways > are definitely going to be database if not dataset dependent. >
for MSSQL, you can do: select top 1 * from table order by newid() `newid` creates a UUID Mike
smime.p7s
Description: S/MIME cryptographic signature
