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 7961873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:10:21+00:00 2026-06-04T05:10:21+00:00

I already saw this post here (http://stackoverflow.com/questions/1398113/sql-select-one-row-randomly-but-taking-into-account-a-weight), but couldnt work it out. Where do

  • 0

I already saw this post here (http://stackoverflow.com/questions/1398113/sql-select-one-row-randomly-but-taking-into-account-a-weight), but couldnt work it out. Where do I put the ‘Stuff’ table? Why don’t they use NEWID() instead of RND()?

Table Stuff

id     item       weight       location
1      ball       1            Wyoming
2      cup        2            Alaska
3      sock       1            Idaho
4      car        3            Miami
5      hot girl   5            Brazil

Now according to that article referenced above I’m supposed to do this

SELECT      TOP 1 t.*
FROM        @Table t
INNER JOIN (SELECT t.id, sum(tt.weight) AS cum_weight
            FROM        @Table t
            INNER JOIN  @Table tt ON  tt.id <= t.id
            GROUP BY    t.id) tc
        ON  tc.id = t.id,
           (SELECT  SUM(weight) AS total_weight FROM @Table) tt,
           (SELECT  RAND() AS rnd) r
WHERE       r.rnd * tt.total_weight <= tc.cum_weight
ORDER BY    t.id ASC

I want to do the above, but in this fashion:

SELECT TOP (1) from stuff WHERE blahblahblah AND (location='Brazil' OR location='Wyoming' OR location='Brazil') AND (weight <= cum_weight) ORDER BY NEWID()

I’m only guessing I can use NEWID() and not compelled to use RND()

  • 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-06-04T05:10:23+00:00Added an answer on June 4, 2026 at 5:10 am

    You could accomplish this by sampling from the cumulative sum, instead of from the records. The idea is to take a cumulative sum of the weights, then to take a random value up to the maximum weight, and finally to see which record has a cumulative sum around that random value. The SQL looks like:

    select top 1 t.*
    from (select t.*, cumulative_sum(weight) as cumweight,
                 sum(weight) over (partition by NULL) as totalweight
          from t
         ) t
    where rand()*(totalweight+1) < cumweight
    order by cumweight desc
    

    What this does is create a cumulative weight and then create a random variable up to the total sum of the weights. The last record whose cum weight is less than the sumweight is chosen. The “+1” is just to be sure that any record could be chosen, even the last.

    In SQL Server 2012, you can calculate the cumulative SUM using SUM() over (partition by NULL order by ).

    In SQL Server 2012, you might use:
    select top 1 t.*
    from (select t., sum(weight) over (partition by NULL order by weight) as cumweight,
    sum(weight) over (partition by NULL) as totalweight
    from t
    ) t
    where rand()
    (totalweight+1) < cumweight
    order by cumweight desc

    Unfortunately, this syntax is not support in SQL Server 2008. In that database, you need to do a self-join, which is the query that you pulled from the original article.

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

Sidebar

Related Questions

I like the pattern I saw in this blog post ( http://marekblotny.blogspot.com/2009/04/conventions-after-rewrite.html ), where
So I saw this post here and read it and it seems like bulk
Before you mark this as answered in another post (I already saw those). But
I saw this post on SO already but it still begs the question, at
I saw a post on this already, but it didn't really provide a solution
I've read a post here about password encoder, and saw this syntax: passwordEncoder.isPasswordValid(user.password, params.password,
I saw this: Detecting if a program is already installed with NSIS . I
I realize there are tonnes of similar questions already up here but I cannot
I already saw this question however, it didn't work for me whatever I tried.
I saw this question already, but I didnt see an answer.. So I get

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.