0

可能重复:
如果行不存在,则 oracle 插入

我正在使用下面的查询来计算某些数据的总数,但是,我希望只有在 EL_CLIENT_REC_UNITS 中首先不存在 ID 时才执行插入。谁能看到这样做的方法?

     INSERT INTO EL_CLIENT_REC_UNITS 
     (ID,
     EL_CLIENT_ID,
     RECORDING_ID,
     UNIT_TYPE,
     PHY_DIG_OR_TOTAL,
     CONTROLLED_PERCENTAGE,
     PERC_ADJ_UNITS_RECEIVED,
     SUM_UNITS_RECEIVED) (       
      (SELECT 
        EL_CLIENT_ID||RECORDING_ID||'CT'   AS ID,
        EL_CLIENT_ID                 AS EL_CLIENT_ID, 
        RECORDING_ID                 AS RECORDING_ID, 
        UNIT_TYPE                    AS UNIT_TYPE, 
        'Total'                      AS PHY_DIG_OR_TOTAL, 
        NULL AS CONTROLLED_PERCENTAGE,
        SUM(PERC_ADJ_UNITS_RECEIVED) AS PERC_ADJ_UNITS_RECEIVED,
        SUM(SUM_UNITS_RECEIVED)      AS SUM_UNITS_RECEIVED
    FROM EL_CLIENT_REC_UNITS
    WHERE EL_CLIENT_ID = pRow.ID
    AND UNIT_TYPE = 'Cleared'
    GROUP BY UNIT_TYPE, EL_CLIENT_ID, RECORDING_ID)
    WHERE NOT EXISTS (SELECT );
4

1 回答 1

1

试试这个

INSERT INTO EL_CLIENT_REC_UNITS 
     (ID,
     EL_CLIENT_ID,
     RECORDING_ID,
     UNIT_TYPE,
     PHY_DIG_OR_TOTAL,
     CONTROLLED_PERCENTAGE,
     PERC_ADJ_UNITS_RECEIVED,
     SUM_UNITS_RECEIVED)       
    SELECT * FROM
    (SELECT 
        EL_CLIENT_ID||RECORDING_ID||'CT'   AS ID,
        EL_CLIENT_ID                 AS EL_CLIENT_ID, 
        RECORDING_ID                 AS RECORDING_ID, 
        UNIT_TYPE                    AS UNIT_TYPE, 
        'Total'                      AS PHY_DIG_OR_TOTAL, 
        NULL AS CONTROLLED_PERCENTAGE,
        SUM(PERC_ADJ_UNITS_RECEIVED) AS PERC_ADJ_UNITS_RECEIVED,
        SUM(SUM_UNITS_RECEIVED)      AS SUM_UNITS_RECEIVED
    FROM EL_CLIENT_REC_UNITS
    WHERE EL_CLIENT_ID = pRow.ID
    AND UNIT_TYPE = 'Cleared'
    GROUP BY UNIT_TYPE, EL_CLIENT_ID, RECORDING_ID) INS_VALS
    WHERE NOT EXISTS (SELECT 1 FROM EL_CLIENT_REC_UNITS ELC WHERE ELC.ID =  INS_VALS.ID );
于 2012-10-02T16:42:01.780 回答