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?