Reputation: 19
I want to get 5 random cases per Agent from a RawData where [Reviewer] = 'G'. If an agent does not have 5 cases that match this criteria then take the required number of random cases where [Reviewer] = 'P'
Raw Data & Example of desired results:
Upvotes: 0
Views: 592
Reputation: 95062
You want to rank an agent's records first by reviewer, then randomly. Use ROW_NUMBER
for this:
select [Date], agent, [Case], accuracy, reviewer
from
(
select
[Date], agent, [Case], accuracy, reviewer,
row_number() over (partition by agent order by reviewer, checksum(newid())) as rn
from mytable
) ranked
where rn <= 5
order by agent, reviewer;
Upvotes: 1