Oracle SQL:根据变量值将一行划分到其他表中的另一行而不遣返

Posted

技术标签:

【中文标题】Oracle SQL:根据变量值将一行划分到其他表中的另一行而不遣返【英文标题】:Oracle SQL: Divided one row to other row in other table without repatriation depending in variable value 【发布时间】:2019-03-06 05:16:40 【问题描述】:

我有拖车桌:

客户

name
-----
TOMMY 
LOUIE 
HUGO 
OLLIE 
DAVID 
LEWIS 
JACKSON 

员工

name     | stage
---------+---------
OLIVER   |  1
NOAH     |  1
ALFIE    |  1
OSCAR    |  2
NOAH     |  2
OLIVER   |  2
LEO      |  2

在员工中,我有两个阶段。在每个阶段,它可以有相同的员工还是不同的员工,我想将客户划分或分配给员工有两个条件:

    第一阶段的客户必须在第二阶段有不同的员工 每个员工在每个阶段必须有相同数量的客户,每个客户在每个阶段必须有 1 名员工,不能重复。

我已经使用游标完成了将结果插入不同表中的过程,但问题是它给出了错误的结果,即在阶段 1 中将客户重复给阶段 2 中的同一员工(例如 NOAH 采用相同的客户)

CREATE  PROCEDURE AUDIT_Customer   AS

CURSOR Customer_STAGE1 IS SELECT * FROM (
                select
                      s.name  Customer_name
                      ,t.name Employees_name 
                from (select name, row_number() over(order by name) as rn from Employees  WHERE stage = 1 ) t
                join (select name, row_number() over(order by name) as rn from Customer  ) s
                on mod(s.rn - 1, (select count(*) from Employees WHERE stage = 1)) = t.rn -1); 


CURSOR Customer_STAGE2 IS SELECT * FROM (
                select
                      s.name  Customer_name
                      ,t.name Employees_name 
                from (select name, row_number() over(order by name) as rn from Employees  WHERE stage = 2 ) t
                join (select name, row_number() over(order by name) as rn from Customer   ) s
                on mod(s.rn - 1, (select count(*) from Employees WHERE stage = 2)) = t.rn -1);                 


Begin 
      For y in  Customer_STAGE1 Loop

             Insert  into Customer_Employee(Customer_name,Employees_name,RECIVE_DATE,stage)
             Values  (Y.Customer_name ,Y.Employees_name,sysdate,1) ;

      End Loop ;

       For y in  Customer_STAGE2 Loop

             Insert  into Customer_Employee(Customer_name,Employees_name,RECIVE_DATE,stage)
             Values  (Y.Customer_name ,Y.Employees_name,sysdate,2) ;

      End Loop ;


   COMMIT;               
End AUDIT_Customer;

结果:

    Customer| Employees| stage
    --------+--------- +---------
    TOMMY   | OLIVER   |  1
    LOUIE   | OLIVER   |  1
    HUGO    | NOAH     |  1
    OLLIE   | NOAH     |  1
    DAVID   | ALFIE    |  1
    LEWIS   | ALFIE    |  1
    JACKSON | ALFIE    |  1
    TOMMY   | OSCAR    |  2
    LOUIE   | OSCAR    |  2
    HUGO    | NOAH     |  2
    OLLIE   | NOAH     |  2
    DAVID   | OLIVER   |  2
    LEWIS   | OLIVER   |  2
    JACKSON | LEO      |  2

我该如何解决?

【问题讨论】:

您使用的是 mysql 还是 Oracle? 使用Oracle sql 【参考方案1】:

过滤程序的第二部分 这可能会有所帮助:

(select name, row_number() over(order by name) as rn from Employees  WHERE stage = 2 and Employee.Name not in (select Employee.Name from Employee WHERE stage = 1))

【讨论】:

它不起作用..它忽略了员工在第 1 阶段和第 2 阶段的情况(例如 NOAH)。

以上是关于Oracle SQL:根据变量值将一行划分到其他表中的另一行而不遣返的主要内容,如果未能解决你的问题,请参考以下文章

oracle中如何获取最后执行的SQL语句并绑定变量值

ORACLE获取SQL绑定变量值的方法总结

保留来自 dataReader c# 的 int 变量值

Oracle:Sql根据同一表另一行的值更新同一表的行

如何将变量值与 select 语句的结果一起放入 sql 表中?

用PL/SQL Developer导出表数据的时候,窗口一闪而过解决办法