SQL random sample with groups

前端 未结 4 1999
天涯浪人
天涯浪人 2021-02-05 18:59

I have a university graduate database and would like to extract a random sample of data of around 1000 records.

I want to ensure the sample is representative of the pop

4条回答
  •  时光取名叫无心
    2021-02-05 19:49

    Add a table for storing population.

    I think it should be like this:

    SELECT *
    FROM (
        SELECT id, coursecode, ROW_NUMBER() OVER (PARTITION BY coursecode ORDER BY NEWID()) AS rn
        FROM degree) t
        LEFT OUTER JOIN
        population p ON t.coursecode = p.coursecode
    WHERE
        rn <= p.SampleSize
    

提交回复
热议问题