我想从[Reviewer] ='G'的RawData中为每个Agent获得5个随机案例。如果座席没有5个符合此条件的个案,则采用所需数量的随机个案,其中[Reviewer] ='P'
原始数据和所需结果示例:
答案 0 :(得分:1)
您要先由审阅者对座席的记录进行排名,然后再对它们进行随机排名。为此,请使用ROW_NUMBER
:
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;