Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:30:16+00:00 2026-05-31T12:30:16+00:00

I have to extract two random records from a table. I have implemented something

  • 0

I have to extract two random records from a table.
I have implemented something like this inside a stored procedure:

    with tmpTable as(
        SELECT top 500 [columns I need]
              , row_number() over(order by [myColumn]) as rown
        FROM SourceTable
          JOIN [myJoin]
        WHERE [myCondition]
    )
    -- here I extract with an intervall of 10 records: 10, 20, 30, ..., 400, 410, ...
    select * from tmpTable where rown = (1 + FLOOR(50*RAND()))*10

It works great, it extracts a random record among the first 500 records from my source.
But, when the sp is called from the presentation layer (ASP.NET 4.0, SqlClient ADO.NET), eventually the same record is returned twice. Note that the two calls are indipendent from each other.

I guess it depends on the fact that the sp is called twice in few milliseconds and that the random generator creates the same number. In the debug process no duplication occurs: due to the F10 manual steps, I guess, that require more than few milliseconds.

How may I obtain two different records?

EDIT

The answer of Lamak requires some more details. The source table is made up of records of products. Groups of about 10 records differ from each other only for some carachteristics (e.g. color). The records are distributed in such a way:

1 to 10: product 1
11 to 20: product 2
... and so on

So if I get the first two random records it is highly expected that the records will regard the same product. This is why I’m using an intervall of 10 records in the random extraction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:30:18+00:00Added an answer on May 31, 2026 at 12:30 pm

    If you are using SQL Server you can just do the following:

    SELECT TOP 1 [columns I need]
    FROM SourceTable
    JOIN [myJoin]
    ON [Something]
    WHERE [MyCondition]
    ORDER BY NEWID()
    

    If you still want to first isolate the records to each product, you can try this:

    SELECT TOP 1 *
    FROM (  SELECT [columns I need], ROW_NUMBER() OVER(PARTITION BY Product ORDER BY NEWID())  Corr
            FROM SourceTable
            JOIN [myJoin]
            ON [Something]
            WHERE [MyCondition]) A
    WHERE Corr = 1
    ORDER BY NEWID()
    

    Though you still can choose a record that has the same product of the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this : oauth_token=1%2F7VDUGD4tKIqSu4jX4DoeCRD1KbqqgTxFnFFliVgbSss&oauth_token_secret=Rk%2FwejMIg6t%2BFphvRd%2BZ5Wkc How can I extract the two
Is there any way to have this: [one[two]][three] And extract this with a regex?
I have to extract columns from a text file explained in this post: Extracting
My problem is :: From a string like /usr/folder1/folder2/filename.ext I have to extract out
I have two cases to extract information from the IDataReader object Case - 1
I have a table containing two columns in SQL that I want to extract
I have a database and i want to extract result based on two table
I am trying to extract character value from UTF-8 format. Suppose I have two
I have two product tables. Table A is the default table (comes from data
I have two databases with equivalent structure and I need to extract data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.