你的病例分布还不错。我会推荐以下方法:
(1) 将顾客分成两组。. . 多案和单案。
(2) 以循环方式将多个案例分配给案例工作者。
(3) 将单个案例分配给个案工作者,以平衡每个个案工作者。
这将适用于您的特定发行版(主要是单例)。它不一定是通用算法。
这是SQL:
with multiples as (
select CustomerId, COUNT(*) CaseCnt,
ROW_NUMBER() over (partition by CustomerId order by CustomerId) % @NumHandlers as theHandler
from customers c
group by CustomerId
having COUNT(*) > 1
),
h as (
select theHandler, SUM(CaseCnt) as CaseCnt,
SUM(CaseCnt) - (NumCusts / @NumHandlers) as StillNeeded
from multiples cross join
(select COUNT(*) as NumCusts from customers c) const
group by theHandler
),
hsum as (
select h.*, SUM(StillNeeded) over (order by StillNeeded) as endnum,
SUM(StillNeeded) over (order by StillNeeded) - StillNeeded + 1 as startnum
from h
),
singles as (
select CustomerId, ROW_NUMBER() over (partition by CustomerId order by CustomerId) as seqnum
from Customers
group by CustomerId
having COUNT(*) = 1
),
singlesh as (
select singles.Customerid, h.theHandler
from singles join
hsum
on singles.seqnum between h.startnum and h.endnum
)
select *
from ((select CustomerId, theHandler
from multiples
) union all
(select CustomerId, theHandler
from singlesh
)
) t
SQL 几乎遵循上面的解释。它首先以循环方式将多条记录随机分配给处理程序。处理程序只是从 0 到 @NumHandlers 的数字。然后它计算“StillNeeded”案例的数量以达到最大值。注意:这假设处理程序的数量是客户数量的精确倍数。修复的调整使查询看起来更复杂。
然后它计算仍然需要为每个处理程序填写的数字。关键是将其作为累积和(这使用 SQL Server 2012 语法,在早期版本中,您使用相关子查询来执行此操作)。然后使用此信息将单例分配给每个处理程序。
最后一步是把union
两组客户放在一起,即多个客户和单身客户。