SQL random sample with groups

前端 未结 4 2037
天涯浪人
天涯浪人 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:29

    I've done similar queries (but not on MS SQL) using a ROW_NUMBER approach:

    select ...
    from 
     ( select ...
         ,row_number() over (partition by coursecode order by newid()) as rn
       from degree
     ) as d 
    join sample size as s
    on d.coursecode = s.coursecode
    and d.rn <= s.samplesize
    

提交回复
热议问题