sql - Oracle SQL - Assign queue of customers to list of Employees
问题描述
I need to distribute list of employees to customers.
For example:
Table 1: List of Employees: A, B & C
Table 2: List of Customers: 1, 2, 3, 4, 5, 6, 7, 8, 9
The needed result:
|------------|------------| | Customers | Employees | |------------|------------| | 1 | A | | 2 | B | | 3 | C | | 4 | A | | 5 | B | | 6 | C | | 7 | A | | 8 | B | | 9 | C | |------------|------------|
解决方案
You can use ROW_NUMBER()
to assign a number on the fly to the employees, and the MOD()
to do the rolling join. For example:
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
推荐阅读
- php - 使用 Cakephp 在 Ajax 请求上随机获取 302
- plot - 使用 pROC 包的特异性/敏感性与截止点
- jenkins - 如何使用 Helm Jenkins 值“CredentialsXmlSecret”
- django - Openedx Django 如何从国家列表中删除一个国家?
- c - AES 密钥生成模板中使用的 CKA_VALUE 是什么?
- java - 在 Java 中使用 JDBC 创建数据库时主要方法无法正常工作
- jsf - 命令按钮在 JSF 1.2 中的数据表中不起作用
- java - 尝试从 url 接收 json 数组时如何删除 404 文件未找到错误?
- javascript - 如何捕获服务器端代码已完成执行?
- php - 使用预签名的 url 获取对象