Need to find a random record from one table, then join another table with this record

StackOverflow https://stackoverflow.com/questions/13419382

  •  29-11-2021
  •  | 
  •  

سؤال

Background: I'm currently developing a simple online quiz. After the user has answered a simple question, the user gets the chance to improve their chances of winning by doing small tasks.

Every lot the user earns is inserted into a separate row, so when I need to find a "random" lot, their chances of winning are increased (please correct me if I'm wrong).

Sample data from the lots table:

LotId      ParticipantId      Created
1          1                  2012-11-16 12:00:00
2          2                  2012-11-16 12:02:00    
3          2                  2012-11-16 12:06:00 
4          2                  2012-11-16 12:15:00    
5          3                  2012-11-16 12:16:00

This means that the participant with ParticipantId 2 has three lots, and therefore a statistically bigger chance of winning compared to the other participants.

What I need to do: When I need to draw a winner, I select a random lot from the lots table. After this I need to find the information on the winner.

Currently I use this following SELECT (any comments on improving the SELECT are appreciated), and then later in my code I have another SELECT with the user's information.

SELECT TOP 1 LotId, ParticipantId FROM Sample_Lots WITH (NOLOCK)
WHERE
    CAST(Created AS DATE) = '2012-11-16'
    AND
    ParticipantId NOT IN
    (
        SELECT ParticipantId FROM Sample_Winners WITH (NOLOCK)
        WHERE
            ParticipantId IS NOT NULL
            AND
            CAST(ThisDate AS DATE) = '2012-11-16'
    )
    AND
    ParticipantId IN
    (
        SELECT ParticipantId FROM Sample_Participants WITH (NOLOCK)
        WHERE
            ParticipationConfirmed IS NOT NULL
    )
ORDER BY NEWID()

I can't work my head around this. I need a random lot, and from this random lot I need to get the user's information in the same SELECT. Any ideas?

هل كانت مفيدة؟

المحلول

Assuming your user information is found in a table named Participants, with columns ParticipantId, FirstName, and LastName, here is an example using a JOIN to that table:

SELECT TOP 1 sl.LotId, sl.ParticipantId, p.FirstName, p.LastName 
FROM Sample_Lots sl 
INNER JOIN Participants p ON sl.ParticipantId = p.ParticipantId WITH (NOLOCK)
WHERE
    CAST(Created AS DATE) = '2012-11-16'
    AND
    sl.ParticipantId NOT IN
    (
        SELECT ParticipantId FROM Sample_Winners WITH (NOLOCK)
        WHERE
            ParticipantId IS NOT NULL
            AND
            CAST(ThisDate AS DATE) = '2012-11-16'
    )
    AND
    sl.ParticipantId IN
    (
        SELECT ParticipantId FROM Sample_Participants WITH (NOLOCK)
        WHERE
            ParticipationConfirmed IS NOT NULL
    )
ORDER BY NEWID()

نصائح أخرى

In your first select, do a left join of Sample_Lots to your Participant table on the ParticipantId columns. Then in your first select you can also get details about the participant. The result will have the same number of rows as your original select and will be ordered randomly.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top