MS-Access:将记录分配给4个人,每个人都有相同的记录数

时间:2014-04-22 18:57:37

标签: sql vba ms-access-2007 grouping partition

任务是说我在cutomer表中有4000(或n)个客户ID,我需要将它们分配给4个人,让他们每个人处理1000(或N / 4)个客户ID, 我试过分区,但它给了我5个结果错误:在每个记录中。

SELECT CUSTOMER_ID, Partition (CUSTOMER_ID, 1, 4, (SELECT ROUND(Sum(B.C)/4,0) AS   
Employee_ID FROM (SELECT CUSTOMER_ID, Count(CUSTOMER_ID) AS C FROM CUSTOMER GROUP BY 
CUSTOMER_NAME) AS B)/4)
FROM CUSTOMER
GROUP BY CUSTOMER_ID;

1 个答案:

答案 0 :(得分:1)

这是一种方法。对于测试数据

CUSTOMER_ID
-----------
          1
          2
          4
          7
          8
         11
         13
         14
         15
...

查询

SELECT c1.CUSTOMER_ID, COUNT(*) AS RankIndex
FROM CUSTOMER c1 INNER JOIN CUSTOMER c2 ON c1.CUSTOMER_ID >= c2.CUSTOMER_ID
GROUP BY c1.CUSTOMER_ID

将产生

CUSTOMER_ID  RankIndex
-----------  ---------
          1          1  
          2          2
          4          3
          7          4
          8          5
         11          6
         13          7
         14          8
         15          9
...

所以,如果我们在COUNT(*)上做一些Modulo 4算术,并将其命名为[Employee_ID],就像这样

SELECT c1.CUSTOMER_ID, ((COUNT(*) - 1) Mod 4) AS Employee_ID
FROM CUSTOMER c1 INNER JOIN CUSTOMER c2 ON c1.CUSTOMER_ID >= c2.CUSTOMER_ID
GROUP BY c1.CUSTOMER_ID

我们得到了

CUSTOMER_ID  Employee_ID
-----------  -----------
          1            0  
          2            1
          4            2
          7            3
          8            0
         11            1
         13            2
         14            3
         15            0
...