We have a table (named QuestionBank) on which every question are stored :
Id Topic Hardness Position
4 1 3 4
5 1 2 1
6 1 1 2
7 1 3 3
8 1 3 4
9 2 2 1
10 2 2 2
11 2 3 3
12 3 1 1
13 3 1 1
14 3 1 2
Every question belongs to a Topic and has it’s own Hardness and Position (every similar question based on their content have same position on corresponding Topic).
Note that the value in Position column is meaningless and it just partitions similar questions in a topic.
The goal is selecting N records with X records of Harness=1 and Y records of Hardness=2 and Z records of Hardness=3 and so on in a random manner; with this constraint that as much as possible there are no records with similar topic and position returned in result set.
For example, the records with Id=4 and Id=8 are similar in either Topic and Position hence one of them are desired to be in result set.
Here’s one way.
Topic, Positionhave multiple questions assign each of these a random orderingRN1.1values forRN1will obviously be ordered first before getting onto2that denotes the second question for a particularTopic, Positioncombo.ROW_NUMBERcalculation partitioned byHardnessto make the finalSELECTeasier..
If you are only selecting a small proportion of the table each time there will likely be much more efficient ways however.