将客户队列分配给员工列表

62o28rlo  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(218)

我需要把员工名单发给客户。
例如:
表1:员工名单:a、b和c
表2:客户列表:1、2、3、4、5、6、7、8、9
所需结果:

|------------|------------|
| Customers  |  Employees |
|------------|------------|
|   1        |     A      |
|   2        |     B      |
|   3        |     C      |
|   4        |     A      |
|   5        |     B      |
|   6        |     C      |
|   7        |     A      |
|   8        |     B      |
|   9        |     C      |
|------------|------------|
guykilcj

guykilcj1#

你可以用 ROW_NUMBER() 在飞行中给员工分配一个号码 MOD() 进行滚动连接。例如:

select
  c.id,
  e.name
from (
  select t.*,
    row_number() over(order by name) as rn
  from employees t
) e
join customers c on e.rn = 
  mod(rn, (select count(*) from customers)) + 1
rkue9o1l

rkue9o1l2#

对所有行进行编号,然后对联接使用模函数:

with e as
(
  select employee, row_number() over (order by employee) as rn
  from employees
)
, c as
(
  select customer, row_number() over (order by customer) as rn
  from customers
)
select c.customer, e.employee
from c
join e on e.rn - 1 = mod(c.rn - 1, (select count(*) from e))
order by c.customer;

演示:https://dbfiddle.uk/?rdbms=oracle_18&fiddle=43a6ca7469dff023d5513fa209e33ea7

owfi6suc

owfi6suc3#

您可以使用mod函数来实现此目的。试试下面的代码。

CREATE TABLE EMP
AS
   SELECT 'A' AS EMP FROM DUAL
   UNION ALL
   SELECT 'B' AS EMP FROM DUAL
   UNION ALL
   SELECT 'C' AS EMP FROM DUAL;

CREATE TABLE CUST
AS
   SELECT '1' AS CUST FROM DUAL
   UNION ALL
   SELECT '2' AS CUST FROM DUAL
   UNION ALL
   SELECT '3' AS CUST FROM DUAL
   UNION ALL
   SELECT '4' AS CUST FROM DUAL
   UNION ALL
   SELECT '5' AS CUST FROM DUAL
   UNION ALL
   SELECT '6' AS CUST FROM DUAL
   UNION ALL
   SELECT '7' AS CUST FROM DUAL
   UNION ALL
   SELECT '8' AS CUST FROM DUAL
   UNION ALL
   SELECT '9' AS CUST FROM DUAL;

  SELECT CUST, EMP
    FROM (SELECT ROW_NUMBER () OVER (PARTITION BY 1 ORDER BY EMP) AS ID, EMP
            FROM EMP) EMP
         INNER JOIN CUST ON MOD (TO_NUMBER (CUST.CUST) - 1, 3) = EMP.ID - 1
ORDER BY 1;

相关问题