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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:03:58+00:00 2026-05-24T02:03:58+00:00

I have the following table: DECLARE @TicketHolder TABLE ( [Name] VARCHAR(100), Tickets DECIMAL(18, 8)

  • 0

I have the following table:

DECLARE @TicketHolder TABLE
(
    [Name] VARCHAR(100),
    Tickets DECIMAL(18, 8)
);

With some test values:

INSERT INTO @TicketHolder ([Name], Tickets)
SELECT 'Bob', 10.37
UNION ALL
SELECT 'Lisa', 1
UNION ALL
SELECT 'Robbie', 150.981
UNION ALL
SELECT 'Cathy', 97.888531;

260.239531 tickets have been bought. Bob owns ~58% of the tickets, Cathy ~38%, and so on:

SELECT [Name], Tickets / (SELECT SUM(Tickets) AS WinProb
FROM @TicketHolder) WinProb FROM @TicketHolder

Now, I need to choose one and only one random winner (using RAND() is good enough) based on how many tickets each person owns. There can be millions of rows, so looping is out of the question. How can a winner be drawn using T-SQL?

  • 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-24T02:03:59+00:00Added an answer on May 24, 2026 at 2:03 am

    This query would find a random number based on the total number of tickets:

    rand() * (select sum(tickets) from @TicketHolder)
    

    You can combine it with a running sum query to find the owner of the block in which the random number was drawn:

    select  top 1 t1.Name
    from    @TicketHolder t1
    where   (
            select  sum(Tickets)
            from    @TicketHolder t2
            where   t2.Name <= t1.Name
            ) > rand() * (select sum(tickets) from @TicketHolder)
    order by
            t1.Name
    

    Example at SE Data. On SE Data, it always selects the same winner, probably because they seed rand() in a fixed way. If I run it locally, it works fine.

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

Sidebar

Related Questions

Let's say I have the following, declare @A table (a int) insert into @A
I have the following table drop table names; create table names( name varchar(200), surname
I have the following: declare @PrintJob TABLE ( PageNumber Int, Copies Int ) INSERT
I have the following script that I want to insert into a table, but
I have the following code: DECLARE @temp_table_1 TABLE (id int identity(0, 1), col_1 varchar(50)),
I have the the following SQL query Declare @tempcalctbl Table ( ItemId varchar(50), ItemLocation
I have the following tables with their insert script: DECLARE @A TABLE ( [ID]
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
I have the following t-sql code which generates an error Declare @table TABLE (

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.