selecting random rows with normal distribution based on a column in SQL Server 2012

Mahdi Hesari

FULL DETAILS: let me explain more clear. this is a table including about 100 question. every question has a BooKRange property that shows from which part of the book, this question hast fetched with values 1,2,3,4. and there is another property called Level that shows level of the difficulty of the question with values 1,2,3,4,5. now i need to randomly select 20 question that have to include all four Book Ranges and all five levels with a normal distribution.

please consider that i need to select distinct rows.

thank you very much.

edit: added the table

CREATE TABLE [dbo].[Question] (
    [QuesID]    INT            IDENTITY (1, 1) NOT NULL,
    [BookRange] NVARCHAR (50)  NULL,
    [Level]     NVARCHAR (50)  NULL,
    PRIMARY KEY CLUSTERED ([QuesID] ASC)
);
Gordon Linoff

You can do this query (assuming a uniform distribution) without doing a union. You just need to specify the ordering correctly.

If you want to select 5 questions from each of the levels, then you can do so by assigning a sequential number to the questions in each level. If these are assigned randomly, then you should meet the requirement of randomness for the levels:

with q as (
      select q.*,
             row_number() over (partition by [range] order by newid()) as seqnum
      from Question q
     )
select *
from q
where seqnum <= 5;

If you want to ensure that these is exactly one question for each level and range, but want the questions random, then do:

with q as (
      select q.*,
             row_number() over (partition by [range], [level] order by newid()) as seqnum
      from Question q
     )
select *
from q
where seqnum = 1;

By the way, range and level are reserved words in SQL Server. In general, it is good practice to avoid using reserved words for the names of things like tables, columns, stored procedures, and so on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

selecting random rows with normal distribution based on a column in SQL Server 2012

From Dev

Update rows based on rownumber in SQL Server 2012

From Dev

Selecting employee based availability Query SQL Server 2012

From Dev

SQL Server 2012 - Update column based on formula

From Dev

Primary Identity Column of table gets random value in sql server 2012

From Dev

Selecting N rows in SQL Server

From Dev

Selecting N rows in SQL Server

From Dev

Random multivariate normal distribution

From Dev

Selecting between quotes (") in SQL Server 2012

From Dev

Selecting Rows Based on Max Column in Group By

From Dev

selecting the rows based on the distinct column values

From Dev

How to speedUp Random selecting in SQL Server

From Dev

SQL - Selecting rows based on date difference

From Dev

INVALID COLUMN? SQL Server 2012

From Dev

Selecting multiple rows in a column into different columns (SQL)

From Dev

SQL Server: Insert Multiple Rows to a table based on a column in a different table

From Dev

SQL Server - transform columns to rows based on 'type-column'

From Dev

Display rows based on column value in sql server 2008

From Dev

Counting Distinct rows based on column using SQL Server

From Dev

SQL Server Query for selecting rows with times based on a specified user-inputted timespan

From Dev

Selecting random rows from pandas dataframe based on counts

From Dev

sql server - selecting top x rows by group

From Dev

SQL Server 2012 Random string from a list

From Java

SQL server getting random number of random rows

From Dev

Selecting one random data from a column from multiple rows in oracle

From Dev

assigning an order ID to different rows of a table based on their Date Order and grouped by CustomerID in SQL Server 2012

From Dev

Compare rows, based on the value (i.e. date and status columns) insert a new row in SQL Server 2012 or SSIS 2012

From Dev

Selecting values from bounded normal distribution in NetLogo

From Dev

Comparing current Month and previous Month's rows on a column, SQL Server 2012

Related Related

  1. 1

    selecting random rows with normal distribution based on a column in SQL Server 2012

  2. 2

    Update rows based on rownumber in SQL Server 2012

  3. 3

    Selecting employee based availability Query SQL Server 2012

  4. 4

    SQL Server 2012 - Update column based on formula

  5. 5

    Primary Identity Column of table gets random value in sql server 2012

  6. 6

    Selecting N rows in SQL Server

  7. 7

    Selecting N rows in SQL Server

  8. 8

    Random multivariate normal distribution

  9. 9

    Selecting between quotes (") in SQL Server 2012

  10. 10

    Selecting Rows Based on Max Column in Group By

  11. 11

    selecting the rows based on the distinct column values

  12. 12

    How to speedUp Random selecting in SQL Server

  13. 13

    SQL - Selecting rows based on date difference

  14. 14

    INVALID COLUMN? SQL Server 2012

  15. 15

    Selecting multiple rows in a column into different columns (SQL)

  16. 16

    SQL Server: Insert Multiple Rows to a table based on a column in a different table

  17. 17

    SQL Server - transform columns to rows based on 'type-column'

  18. 18

    Display rows based on column value in sql server 2008

  19. 19

    Counting Distinct rows based on column using SQL Server

  20. 20

    SQL Server Query for selecting rows with times based on a specified user-inputted timespan

  21. 21

    Selecting random rows from pandas dataframe based on counts

  22. 22

    sql server - selecting top x rows by group

  23. 23

    SQL Server 2012 Random string from a list

  24. 24

    SQL server getting random number of random rows

  25. 25

    Selecting one random data from a column from multiple rows in oracle

  26. 26

    assigning an order ID to different rows of a table based on their Date Order and grouped by CustomerID in SQL Server 2012

  27. 27

    Compare rows, based on the value (i.e. date and status columns) insert a new row in SQL Server 2012 or SSIS 2012

  28. 28

    Selecting values from bounded normal distribution in NetLogo

  29. 29

    Comparing current Month and previous Month's rows on a column, SQL Server 2012

HotTag

Archive