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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:51:31+00:00 2026-05-27T19:51:31+00:00

We have a table (named QuestionBank) on which every question are stored : Id

  • 0

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.

  • 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-27T19:51:32+00:00Added an answer on May 27, 2026 at 7:51 pm

    Here’s one way.

    1. First if any Topic, Position have multiple questions assign each of these a random ordering RN1.
    2. Then sort by that but randomly within each group. All the 1 values for RN1 will obviously be ordered first before getting onto 2 that denotes the second question for a particular Topic, Position combo.
    3. Then use the result of that in a ROW_NUMBER calculation partitioned by Hardness to make the final SELECT easier.

    .

    WITH T1
         AS (SELECT *,
                    Row_number() OVER (PARTITION BY Topic, Position 
                                           ORDER BY Newid()) AS RN1
             FROM   QuestionBank),
         T2
         AS (SELECT *,
                    Row_number() OVER (ORDER BY RN1, Newid()) AS RN2
             FROM   T1),
         T3
         AS (SELECT *,
                    Row_number() OVER (PARTITION BY Hardness 
                                           ORDER BY RN2) AS RN3
             FROM   T2)
    SELECT Id,
           Topic,
           Hardness,
           Position
    FROM   T3
    WHERE  ( Hardness = 1
             AND RN3 <= 3 )
            OR ( Hardness = 2
                 AND RN3 <= 2 )
            OR ( Hardness = 3
                 AND RN3 <= 2 )
    ORDER  BY Topic,
              Position,
              Hardness 
    

    If you are only selecting a small proportion of the table each time there will likely be much more efficient ways however.

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

Sidebar

Related Questions

I have table named Schedule which has fields named TeacherName and ClassTakenDate.The values in
I have a table named tbl.Products , which has a column named articlenumber and
I have a table named department , which has following data DNO DNAME SALARY
I have table named as contacts which has nearly 1.2 million records we use
I have a table named OT Hours which has the following column EmpId, Date,
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
I have a database table (named Topics) which includes these fields : topicId name
I have a table named Template which I set my messages to send them
I have a table named users and each row (user) has an image. The
I have a table named b having a column j which is varchar(3) where

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.