带有rowover分区的Oracle SQL Update

时间:2016-08-23 19:17:20

标签: sql oracle window-functions

为什么我得到B1.EMPLOYEE_ID是SQL下面的无效标识符的错误:

UPDATE TABLE_1 A1 
  SET A1.ADDL_SEQ = ( SELECT SEQ 
                        FROM (SELECT ROW_NUMBER() OVER (PARTITION BY EMPLOYEE_ID 
                                                        ORDER BY EMPLOYEE_ID, ERNCD) SEQ
                                FROM TABLE_1) B1 
                       WHERE B1.EMPLOYEE_ID=A1.EMPLOYEE_ID );

3 个答案:

答案 0 :(得分:2)

您也可以像这样编写更新语句:

UPDATE 
   (SELECT EMPLOYEE_ID, ADDL_SEQ, 
        ROW_NUMBER() OVER (PARTITION BY EMPLOYEE_ID ORDER BY EMPLOYEE_ID, ERNCD) AS SEQ
   FROM TABLE_1)
SET ADDL_SEQ = SEQ;

答案 1 :(得分:0)

您应该将EMPLOYEE_ID添加到B1内部查询:

ROWSIZE*sizeof(ITEM_SIZE)

答案 2 :(得分:-1)

添加一个case语句,就像这样

 UPDATE TABLE_1 A1 
      SET A1.ADDL_SEQ = ( SELECT  
                            (CASE WHEN  employee_id = ERNCD THEN seq ELSE NULL END) AS SEQ
                            FROM (SELECT ROW_NUMBER() OVER (PARTITION BY EMPLOYEE_ID 
                                                            ORDER BY EMPLOYEE_ID, ERNCD) SEQ
                                    FROM TABLE_1) B1 
                           WHERE B1.EMPLOYEE_ID=A1.EMPLOYEE_ID );

我认为你不需要WHERE条件但是很难看到数据