0

任务是说我在客户表中有 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;
4

1 回答 1

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
...

所以如果我们对它进行一些模 4 运算COUNT(*)并将其命名为 [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
...
于 2014-04-22T20:32:42.167 回答