January 27, 2023 at 8:41 pm
hi,
I have a data like below where i need to randomize data by review type
SInce i have sample size as 5. I need to pull 1 record for each review type until sample size is met
so, i will be pulling
please let me know. how can i achieve this ?
January 28, 2023 at 2:30 am
Presumably there are other employees besides Betty? If so it would be convenient to start with a table containing (unique) employees and the sample size. In the case where you're only interested in Betty you could maybe try something like this
declare
@employee nvarchar(54)=N'Betty',
@sample int=5;
with
rand_cte as (
select *, row_number() over (partition by review_type
order by newid()) rand_rn
from your_table
where employee=@employee)
select top(@sample) *
from rand_cte
order by rand_rn;
The ROW_NUMBER function uses NEWID to randomly order the partitioned rows. Per your example the code selects all of the rows where rand_rn=1 (there are 3), then all of the rows where rand_rn=2 (there are 2), etc. until the sample total is reached. It uses TOP(n) and ORDER BY rand_rn to select across the partitioned random numbers
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
February 7, 2023 at 6:18 am
This was removed by the editor as SPAM
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply