如何使用联接更新组汇总表数据

时间:2015-05-28 08:30:47

标签: sql oracle oracle10g

以下是我的更新查询:

UPDATE   (  SELECT   BTD_ITEM_CODE,
                 INVI_ITEM_CODe,
                 BTD_UOM_CODE,
                 INVI_UOM_CODE,
                 BTD_GRADE_CODE,
                 INVI_GRADE_CODE_1,
                 SUM (BTD_QTY) BTD_QTY,
                 SUM (INVI_QTY) INVI_QTY,
                 SUM (BTD_QTY_LS) BTD_QTY_LS,
                 SUM (INVI_QTY_LS) INVI_QTY_LS,
                 SUM (BTD_QTY_BU) BTD_QTY_BU,
                 SUM (INVI_QTY_BU) INVI_QTY_BU
          FROM            AA_BIN_TRANSACTIONS_DET
                       INNER JOIN
                          AA_BIN_TRANSACTIONS_HEAD
                       ON AA_BIN_TRANSACTIONS_HEAD.BTH_SYS_ID =
                             AA_BIN_TRANSACTIONS_DET.BTD_BTH_SYS_ID
                    INNER JOIN
                       OT_INVOICE_HEAD
                    ON AA_BIN_TRANSACTIONS_HEAD.BTH_TXN_CODE =
                          OT_INVOICE_HEAD.INVH_TXN_CODE
                       AND AA_BIN_TRANSACTIONS_HEAD.BTH_NO =
                             OT_INVOICE_HEAD.INVH_NO
                 INNER JOIN
                    OT_INVOICE_ITEM
                 ON OT_INVOICE_ITEM.INVI_INVH_SYS_ID =
                       OT_INVOICE_HEAD.INVH_SYS_ID
                    AND AA_BIN_TRANSACTIONS_DET.BTD_ITEM_CODE =
                          OT_INVOICE_ITEM.INVI_ITEM_CODE
                    AND AA_BIN_TRANSACTIONS_DET.BTD_UOM_CODE =
                          OT_INVOICE_ITEM.INVI_UOM_CODE
                    AND AA_BIN_TRANSACTIONS_DET.BTD_GRADE_CODE =
                          OT_INVOICE_ITEM.INVI_GRADE_CODE_1
      GROUP BY   BTD_ITEM_CODE, BTD_UOM_CODE, BTD_GRADE_CODE)
   SET   INVI_QTY = BTD_QTY,
         INVI_QTY_LS = BTD_QTY_LS,
         INVI_QTY_BU = BTD_QTY_BU
 WHERE       INVI_ITEM_CODE = BTD_ITEM_CODE
         AND INVI_UOM_CODE = BTD_UOM_CODE
         AND INVI_GRADE_CODE_1 = BTD_GRADE_CODE;

我需要使用OT_INVOICE_ITEM中的值更新详细表AA_BIN_TRANSACTIONS_DET中的值。需要将3个值BTD_QTYBTD_QTY_LSBTD_QTY_BU相加,因为表中有多个事务。详细信息表中的唯一标识符为BTD_ITEM_CODE。事务代码和编号对于标头表OT_INVOICE_HEADAA_BIN_TRANSACTIONS_HEAD都是通用的。两个表的标题和详细表也需要连接。最后,我们从AA_BIN_TRANSACTIONS_DET获得的总和值需要更新为OT_INVOICE_ITEM

我尝试了上面的代码,但错误ORA-01732: data manipulation operation not legal on this view。我不明白为什么。

INVI_ITEM_CODE应与BTD_ITEM_CODE匹配,INVI_UOM_CODE应与BTD_UOM_CODE匹配且INVI_GRADE_CODE_1应与BTD_GRADE_CODE匹配,则只应更新记录。< / p>

提前致谢。

3 个答案:

答案 0 :(得分:0)

如果以下查询有帮助,请与我们联系 -

            with t1 as 
  (  SELECT   BTD_ITEM_CODE,
                 INVI_ITEM_CODe,
                 BTD_UOM_CODE,
                 INVI_UOM_CODE,
                 BTD_GRADE_CODE,
                 INVI_GRADE_CODE_1,
                 SUM (BTD_QTY) BTD_QTY,
                 SUM (INVI_QTY) INVI_QTY,
                 SUM (BTD_QTY_LS) BTD_QTY_LS,
                 SUM (INVI_QTY_LS) INVI_QTY_LS,
                 SUM (BTD_QTY_BU) BTD_QTY_BU,
                 SUM (INVI_QTY_BU) INVI_QTY_BU
          FROM            AA_BIN_TRANSACTIONS_DET
                       INNER JOIN
                          AA_BIN_TRANSACTIONS_HEAD
                       ON AA_BIN_TRANSACTIONS_HEAD.BTH_SYS_ID =
                             AA_BIN_TRANSACTIONS_DET.BTD_BTH_SYS_ID
                    INNER JOIN
                       OT_INVOICE_HEAD
                    ON AA_BIN_TRANSACTIONS_HEAD.BTH_TXN_CODE =
                          OT_INVOICE_HEAD.INVH_TXN_CODE
                       AND AA_BIN_TRANSACTIONS_HEAD.BTH_NO =
                             OT_INVOICE_HEAD.INVH_NO
                 INNER JOIN
                    OT_INVOICE_ITEM
                 ON OT_INVOICE_ITEM.INVI_INVH_SYS_ID =
                       OT_INVOICE_HEAD.INVH_SYS_ID
                    AND AA_BIN_TRANSACTIONS_DET.BTD_ITEM_CODE =
                          OT_INVOICE_ITEM.INVI_ITEM_CODE
                    AND AA_BIN_TRANSACTIONS_DET.BTD_UOM_CODE =
                          OT_INVOICE_ITEM.INVI_UOM_CODE
                    AND AA_BIN_TRANSACTIONS_DET.BTD_GRADE_CODE =
                          OT_INVOICE_ITEM.INVI_GRADE_CODE_1
      GROUP BY   BTD_ITEM_CODE,
                 INVI_ITEM_CODe,
                 BTD_UOM_CODE,
                 INVI_UOM_CODE,
                 BTD_GRADE_CODE,
                 INVI_GRADE_CODE_1)
Update T1
   SET   INVI_QTY = BTD_QTY,
         INVI_QTY_LS = BTD_QTY_LS,
         INVI_QTY_BU = BTD_QTY_BU

答案 1 :(得分:0)

我通过以下查询完成了它,它是一个非常长且重复的查询但是有没有优化或简短的查询来做到这一点?

UPDATE   OT_INVOICE_ITEM
   SET   INVI_QTY =
            (SELECT   SUM (BTD_QTY) BTD_QTY
               FROM            AA_BIN_TRANSACTIONS_DET
                            INNER JOIN
                               AA_BIN_TRANSACTIONS_HEAD
                            ON AA_BIN_TRANSACTIONS_HEAD.BTH_SYS_ID =
                                  AA_BIN_TRANSACTIONS_DET.BTD_BTH_SYS_ID
                         INNER JOIN
                            OT_INVOICE_HEAD
                         ON AA_BIN_TRANSACTIONS_HEAD.BTH_TXN_CODE =
                               OT_INVOICE_HEAD.INVH_TXN_CODE
                            AND AA_BIN_TRANSACTIONS_HEAD.BTH_NO =
                                  OT_INVOICE_HEAD.INVH_NO
                      INNER JOIN
                         OT_INVOICE_ITEM
                      ON OT_INVOICE_ITEM.INVI_INVH_SYS_ID =
                            OT_INVOICE_HEAD.INVH_SYS_ID
                         AND AA_BIN_TRANSACTIONS_DET.BTD_ITEM_CODE =
                               OT_INVOICE_ITEM.INVI_ITEM_CODE
                         AND AA_BIN_TRANSACTIONS_DET.BTD_UOM_CODE =
                               OT_INVOICE_ITEM.INVI_UOM_CODE
                         AND AA_BIN_TRANSACTIONS_DET.BTD_GRADE_CODE =
                               OT_INVOICE_ITEM.INVI_GRADE_CODE_1),
         INVI_QTY_LS =
            (SELECT   SUM (BTD_QTY_LS) BTD_QTY_LS
               FROM            AA_BIN_TRANSACTIONS_DET
                            INNER JOIN
                               AA_BIN_TRANSACTIONS_HEAD
                            ON AA_BIN_TRANSACTIONS_HEAD.BTH_SYS_ID =
                                  AA_BIN_TRANSACTIONS_DET.BTD_BTH_SYS_ID
                         INNER JOIN
                            OT_INVOICE_HEAD
                         ON AA_BIN_TRANSACTIONS_HEAD.BTH_TXN_CODE =
                               OT_INVOICE_HEAD.INVH_TXN_CODE
                            AND AA_BIN_TRANSACTIONS_HEAD.BTH_NO =
                                  OT_INVOICE_HEAD.INVH_NO
                      INNER JOIN
                         OT_INVOICE_ITEM
                      ON OT_INVOICE_ITEM.INVI_INVH_SYS_ID =
                            OT_INVOICE_HEAD.INVH_SYS_ID
                         AND AA_BIN_TRANSACTIONS_DET.BTD_ITEM_CODE =
                               OT_INVOICE_ITEM.INVI_ITEM_CODE
                         AND AA_BIN_TRANSACTIONS_DET.BTD_UOM_CODE =
                               OT_INVOICE_ITEM.INVI_UOM_CODE
                         AND AA_BIN_TRANSACTIONS_DET.BTD_GRADE_CODE =
                               OT_INVOICE_ITEM.INVI_GRADE_CODE_1),
         INVI_QTY_BU =
            (SELECT   SUM (BTD_QTY_BU) BTD_QTY_BU
               FROM            AA_BIN_TRANSACTIONS_DET
                            INNER JOIN
                               AA_BIN_TRANSACTIONS_HEAD
                            ON AA_BIN_TRANSACTIONS_HEAD.BTH_SYS_ID =
                                  AA_BIN_TRANSACTIONS_DET.BTD_BTH_SYS_ID
                         INNER JOIN
                            OT_INVOICE_HEAD
                         ON AA_BIN_TRANSACTIONS_HEAD.BTH_TXN_CODE =
                               OT_INVOICE_HEAD.INVH_TXN_CODE
                            AND AA_BIN_TRANSACTIONS_HEAD.BTH_NO =
                                  OT_INVOICE_HEAD.INVH_NO
                      INNER JOIN
                         OT_INVOICE_ITEM
                      ON OT_INVOICE_ITEM.INVI_INVH_SYS_ID =
                            OT_INVOICE_HEAD.INVH_SYS_ID
                         AND AA_BIN_TRANSACTIONS_DET.BTD_ITEM_CODE =
                               OT_INVOICE_ITEM.INVI_ITEM_CODE
                         AND AA_BIN_TRANSACTIONS_DET.BTD_UOM_CODE =
                               OT_INVOICE_ITEM.INVI_UOM_CODE
                         AND AA_BIN_TRANSACTIONS_DET.BTD_GRADE_CODE =
                               OT_INVOICE_ITEM.INVI_GRADE_CODE_1)

答案 2 :(得分:0)

请尝试这个

Update OT_INVOICE_ITEM Z
   SET  ( INVI_QTY, INVI_QTY_LS, INVI_QTY_BU )   = ( 
WITH T1 as (
SELECT   BTD_ITEM_CODE, BTD_UOM_CODE, BTD_GRADE_CODE, INVH_SYS_ID,
                 SUM (BTD_QTY) BTD_QTY,
                 SUM (BTD_QTY_LS) BTD_QTY_LS,
                 SUM (BTD_QTY_BU) BTD_QTY_BU,

FROM      AA_BIN_TRANSACTIONS_DET A
INNER JOIN
          AA_BIN_TRANSACTIONS_HEAD B
ON        B.BTH_SYS_ID = A.BTD_BTH_SYS_ID
INNER JOIN
          OT_INVOICE_HEAD C
ON        B.BTH_TXN_CODE = C.INVH_TXN_CODE
AND       B.BTH_NO = C.INVH_NO

Group by BTD_ITEM_CODE, BTD_UOM_CODE, BTD_GRADE_CODE, INVH_SYS_ID
)
SELECT BTD_QTY, BTD_QTY_LS,
           BTD_QTY_BU
FROM   T1
WHERE (BTD_ITEM_CODE, BTD_UOM_CODE, BTD_GRADE_CODE, INVH_SYS_ID) =
(Z.INVI_ITEM_CODE, Z.INVI_UOM_CODE, Z.INVI_GRADE_CODE_1, Z.INVI_INVH_SYS_ID)
and exists
  ( Select 1
    From t1
    WHERE (BTD_ITEM_CODE, BTD_UOM_CODE, BTD_GRADE_CODE, INVH_SYS_ID) =
    (Z.INVI_ITEM_CODE, Z.INVI_UOM_CODE, Z.INVI_GRADE_CODE_1, Z.INVI_INVH_SYS_ID)
)
)