Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2006
    Posts
    111

    Unanswered: select random rows

    is there a way to do the above? i found solutions but limited to a single row only and i don't want to do a 'cursor'-like solution, having to iterate. also my requirement is the select includes a where clause something like below:

    Code:
    select
        *
    from
        myTable
    where
        dateReceived between @startDate and @endDate
    and get say, 10 random rows from the result set.

    i've tried the order by rand() but having run it a couple of times (in intervals greater than clock ticks) i get the same set of results.

    thanks in advance!

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    ORDER BY NewId()
    -PatP

  3. #3
    Join Date
    Jul 2006
    Posts
    111

    update...

    currently i have the following:

    Code:
    declare @milliSecNow int
    set @milliSecNow = datepart(Ms, getdate())
    print milliSecNow
    
    select
        top 100000 *
    into
        #randByDate
    from
        myTable
    where
        recv_date between '2007-01-01' AND '2007-12-31'
    --order by
    --    rand(@milliSecNow)
    
    select
        top 10 firstColumn
    from
        #randByDate
    order by
        rand(@milliSecNow)
    
    drop table #randByDate

  4. #4
    Join Date
    Jul 2006
    Posts
    111

    ...

    *imagine montgomery burns*... eeeexcellent!

    Quote Originally Posted by Pat Phelan
    Code:
    ORDER BY NewId()
    -PatP

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    As a matter of fact, Pat does look a bit like Montgomery Burns. Not much imagination required...
    ...D'oh!
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •