我有一个看起来像这样的集合:
staffID clientID eventID
s1 c1 e1c1
s1 c1 e2c1
s1 c2 e1c2
s2 c3 e1c3
s2 c4 e1c4
s2 c5 e1c5
s2 c6 e1c6
我想从这个列表中为每个员工选择两个随机客户端。客户不应重复。这不是s2的问题,但对于s1可能是个问题: s1 c1 e1c1 s1 c1 e2c1 s2 c3 e1c3 s2 c5 e1c5 如果客户有一个员工有多个事件,客户可能会重复。
我的代码很有效,除了这一方面(上图)。
SELECT *
FROM (
SELECT eru AS RU, rudesc AS RUName, estaff AS StaffID
, (CASE
WHEN cdprogram.prog = client.cprog THEN ('yes: ' + cdprogram.prog)
WHEN cdprogram.prog <> client.cprog THEN ('no: ' + client.cprog)
END ) AS 'IsPrimProg'
, ROWNUMBER() OVER (PARTITION BY eru, estaff ORDER BY NEWID()) 'RowsPerStaff'
FROM staff INNER JOIN events ON sid = estaff
LEFT OUTER JOIN client ON client.cid = events.ecaseno
WHERE ...
GROUP BY eru, rudesc, estaff, sfname, slname, cdprogram.prog, client.cprog
, ecaseno, edate, cdprogram.progdesc, eser
) innerquery
WHERE innerquery.RowsPerStaff <= 2
ORDER BY RU, StaffID, RowsPerStaff
这段代码效果很好,它为每个员工生成两行。但是,现在我需要修复它,以便不会选择同一个客户端两次。
谢谢大家!