CREATE OR REPLACE PROCEDURE sp_cust_main_data_yx(InStrDate  IN VARCHAR2,
                                                 OS_ERR_MSG OUT VARCHAR2) AS

BEGIN
  --1、清空临时表数据
  execute immediate 'truncate table DATA_SALE_DAY_ITEM';
  execute immediate 'truncate table DATA_SALE_DAY_TP';
  execute immediate 'truncate table DATA_SALE_DAY';
  execute immediate 'truncate table DATA_SALE_MONTH_TP';
  execute immediate 'truncate table DATA_SALE_MONTH';

--2、跑出结果到临时表
  --交易明细数据
  INSERT INTO DATA_SALE_DAY_ITEM
    SELECT TO_DATE(A.CRMPOSTDAT, 'yyyymmdd') p_date,
           TO_DATE(DECODE(A."/BIC/ZTCRMC01",
                          '00000000',
                          NULL,
                          ' ',
                          NULL,
                          A."/BIC/ZTCRMC01"),
                   'yyyymmdd') order_date,
           A."CRM_ITMTYP" order_type,
           A."/BIC/ZKUNNR_L1" order_channel_l1,
           D.TXTSH channel_l1_name, --
           A."/BIC/ZKUNNR_L2" order_channel_l2,
           E.TXTSH channel_l2_name, --
           A.CRM_OBJ_ID crm_order_id,
           A."/BIC/ZTCRMC04" order_no,
           A.CRM_ENDCST cust_no,
           A."/BIC/ZTCMC016" cust_grade,
           LTRIM(A."/BIC/ZMATERIAL", '0') ITEM_CODE,
           C.TXTMD item_name,
           LTRIM(B."/BIC/ZMATDL", '0') CATE_B, --大类
           LTRIM(B."/BIC/ZMATZL", '0') CATE_M, --中类
           LTRIM(B."/BIC/ZMATXL", '0') CATE_S, --小类
           LTRIM(B.MATL_GROUP, '0') CATE_D, --细类
           A."/BIC/ZEAMC001" unit_no,
           TO_DATE(decode(A."/BIC/ZEAMC011" || A."/BIC/ZEAMC013",
                          '00000000000000',
                          null,
                          ' 00000',
                          null,
                          A."/BIC/ZEAMC011" || A."/BIC/ZEAMC013"),
                   'yyyymmddhh24miss') unit_begin_time,
           TO_DATE(decode(A."/BIC/ZEAMC012" || A."/BIC/ZEAMC014",
                          '00000000000000',
                          null,
                          ' 00000',
                          null,
                          A."/BIC/ZEAMC012" || A."/BIC/ZEAMC014"),
                   'yyyymmddhh24miss') unit_end_time,
           LTRIM(A."/BIC/ZTCMC020", '0') addr_no,
           H."/BIC/ZTCMC017" province, --
           H1.TXTSH province_name, --
           DECODE(A."/BIC/ZCRMD015", 'X', 1, 0) is_related_sale,
           DECODE(A."/BIC/ZCRMD016", 'X', 1, 0) is_ivr,
           A."/BIC/ZCRMD018" order_source,
           A."/BIC/ZEAMC027" md_code,
           SUBSTR(A."/BIC/ZPST_TIM", 1, 2) sale_period,
           --总订购
           DECODE(A.CRM_PRCTYP, 'ZA01', 1, 'ZA02', 1, 0) *
           A."/BIC/ZAMK0010" tot_order_qty,
           DECODE(A.CRM_PRCTYP, 'ZA01', 1, 'ZA02', 1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."/BIC/ZAMK0011" tot_order_amt,
           DECODE(A.CRM_PRCTYP, 'ZA01', 1, 'ZA02', 1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."CRM_SRVKB" tot_order_cost,
           --净订购
           DECODE(A.CRM_PRCTYP, 'ZA01', 1, 'ZA02', 1, 'ZCR1', -1, 0) *
           A."/BIC/ZAMK0010" net_order_qty,
           DECODE(A.CRM_PRCTYP, 'ZA01', 1, 'ZA02', 1, 'ZCR1', -1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."/BIC/ZAMK0011" net_order_amt,
           DECODE(A.CRM_PRCTYP, 'ZA01', 1, 'ZA02', 1, 'ZCR1', -1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."CRM_SRVKB" net_order_cost,
           --有效订购
           DECODE(A.CRM_PRCTYP,
                  'ZA01',
                  1,
                  'ZA02',
                  1,
                  'ZCR1',
                  -1,
                  'ZB01',
                  -1,
                  0) * A."/BIC/ZAMK0010" effect_order_qty,
           DECODE(A.CRM_PRCTYP,
                  'ZA01',
                  1,
                  'ZA02',
                  1,
                  'ZCR1',
                  -1,
                  'ZB01',
                  -1,
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) *
           A."/BIC/ZAMK0011" effect_order_amt,
           DECODE(A.CRM_PRCTYP,
                  'ZA01',
                  1,
                  'ZA02',
                  1,
                  'ZCR1',
                  -1,
                  'ZB01',
                  -1,
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) *
           A."/BIC/ZCRMK009" effect_price_amt,
           DECODE(A.CRM_PRCTYP,
                  'ZA01',
                  1,
                  'ZA02',
                  1,
                  'ZCR1',
                  -1,
                  'ZB01',
                  -1,
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."CRM_SRVKB" effect_order_cost,
           DECODE(A.CRM_PRCTYP,
                  'ZA01',
                  1,
                  'ZA02',
                  1,
                  'ZCR1',
                  -1,
                  'ZB01',
                  -1,
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) *
           A."/BIC/ZCRMK006" effect_discount_amt,
           --取消
           DECODE(A.CRM_PRCTYP, 'ZCR1', 1, 0) * A."/BIC/ZAMK0010" order_cancel_qty,
           DECODE(A.CRM_PRCTYP, 'ZCR1', 1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."/BIC/ZAMK0011" order_cancel_amt,
           --拒收
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 1, 0),
                  0) * A."/BIC/ZAMK0010" order_reject_qty,
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 1, 0),
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) *
           A."/BIC/ZAMK0011" order_reject_amt,
           --退货
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 0, 1),
                  0) * A."/BIC/ZAMK0010" order_refund_qty,
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 0, 1),
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) *
           A."/BIC/ZAMK0011" order_refund_amt,
           --拒收取消
           0 order_reject_cancel_qty,
           0 order_reject_cancel_amt,
           --退货取消
           0 order_refund_cancel_qty,
           0 order_refund_cancel_amt
      FROM ITF_SAP.SAP_BIC_AZTCRD00100 A,
           ITF_SAP.SAP_BIC_PZMATERIAL  B,
           ITF_SAP.SAP_BIC_TZMATERIAL  C,
           ITF_SAP."/BIC/OHZO_MSCODE"  D,
           ITF_SAP."/BIC/OHZO_MSGB"    E,
           ITF_SAP."/BIC/OHZO_PZHEN"   H,
           ITF_SAP."/BIC/OHZO_TSHENG"  H1
     WHERE A.CRMPOSTDAT BETWEEN '20060301' AND InStrDate
       AND NVL(A."/BIC/ZCRMD001", ' ') <> 'TA10' --剔除预订购
       AND NVL(A."/BIC/ZCRMD048", ' ') <> 'TANN' --剔除赠品
       AND A."/BIC/ZCRMD199" IS NULL --剔除预订购
       AND A."/BIC/ZMATERIAL" = B."/BIC/ZMATERIAL"(+)
       AND A."/BIC/ZMATERIAL" = C."/BIC/ZMATERIAL"(+)
       AND A."/BIC/ZKUNNR_L1" = D."/BIC/ZKUNNR_L1"(+)
       AND D.LANGU(+) = '1'
       AND A."/BIC/ZKUNNR_L2" = E."/BIC/ZKUNNR_L2"(+)
       AND E.LANGU(+) = '1'
       AND A."/BIC/ZTCMC020" = H."/BIC/ZTCMC020"(+)
       AND H."/BIC/ZTCMC017" = H1."/BIC/ZTCMC017"(+)
    UNION ALL
    SELECT to_date(A."/BIC/ZTCMC021", 'yyyymmdd') 过账日期,
           TO_DATE(DECODE(A."/BIC/ZTCRMC01",
                          '00000000',
                          NULL,
                          ' ',
                          NULL,
                          A."/BIC/ZTCRMC01"),
                   'yyyymmdd') CRM订购日期_权责制,
           A."CRM_ITMTYP" 项目交易类型,
           A."/BIC/ZKUNNR_L1" order_channel_l1,
           D.TXTSH channel_l1_name, --
           A."/BIC/ZKUNNR_L2" order_channel_l2,
           E.TXTSH channel_l2_name, --
           A.CRM_OBJ_ID 交易编号,
           A."/BIC/ZTCRMC04" "CRM订单编号_权责制",
           A.CRM_ENDCST 会员,
           A."/BIC/ZTCMC016" 会员等级,
           LTRIM(A."/BIC/ZMATERIAL", '0') ITEM_CODE,
           C.TXTMD item_name,
           LTRIM(B."/BIC/ZMATDL", '0') CATE_B, --大类
           LTRIM(B."/BIC/ZMATZL", '0') CATE_M, --中类
           LTRIM(B."/BIC/ZMATXL", '0') CATE_S, --小类
           LTRIM(B.MATL_GROUP, '0') CATE_D, --细类
           A."/BIC/ZEAMC001" unit_no,
           TO_DATE(decode(A."/BIC/ZEAMC011" || A."/BIC/ZEAMC013",
                          '00000000000000',
                          null,
                          ' 00000',
                          null,
                          A."/BIC/ZEAMC011" || A."/BIC/ZEAMC013"),
                   'yyyymmddhh24miss') unit_begin_time,
           TO_DATE(decode(A."/BIC/ZEAMC012" || A."/BIC/ZEAMC014",
                          '00000000000000',
                          null,
                          ' 00000',
                          null,
                          A."/BIC/ZEAMC012" || A."/BIC/ZEAMC014"),
                   'yyyymmddhh24miss') unit_end_time,
           LTRIM(A."/BIC/ZTCMC020", '0') addr_no,
           H."/BIC/ZTCMC017" province, --
           H1.TXTSH province_name, --
           DECODE(A."/BIC/ZCRMD015", 'X', 1, 0) is_related_sale,
           DECODE(A."/BIC/ZCRMD016", 'X', 1, 0) is_ivr,
           A."/BIC/ZCRMD018" order_source,
           A."/BIC/ZEAMC027" md_code,
           SUBSTR(A."/BIC/ZPST_TIM", 1, 2) sale_period,
           --总订购
           0 总订购件数,
           0 总订购金额,
           0 总订购成本,
           --净订购
           0 净订购件数,
           0 净订购金额,
           0 净订购成本,
           --有效订购
           DECODE(A.CRM_PRCTYP, 'ZB01', 1, 0) * A."/BIC/ZAMK0010" 有效订购件数,
           DECODE(A.CRM_PRCTYP, 'ZB01', 1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."/BIC/ZAMK0011" 有效订购金额,
           DECODE(A.CRM_PRCTYP, 'ZB01', 1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."/BIC/ZCRMK009" 有效售价金额,
           DECODE(A.CRM_PRCTYP, 'ZB01', 1, 0) *
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."CRM_SRVKB" 有效订购成本,
           DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) * A."/BIC/ZCRMK006" "有效折扣金额",
           --取消
           0 取消订购件数,
           0 取消订购金额,
           --拒收
           0 拒收订购件数,
           0 拒收订购金额,
           --退货
           0 退货订购件数,
           0 退货订购金额,
           --拒收取消
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 1, 0),
                  0) * A."/BIC/ZAMK0010" 拒收取消订购件数,
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 1, 0),
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) *
           A."/BIC/ZAMK0011" 拒收取消订购金额,
           --退货取消
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 0, 1),
                  0) * A."/BIC/ZAMK0010" 退货取消订购件数,
           DECODE(A.CRM_PRCTYP,
                  'ZB01',
                  DECODE(A."/BIC/ZTCMC008", '20', 0, 1),
                  0) * DECODE(A.CRM_ITMTYP, 'TANN', 0, 1) *
           A."/BIC/ZAMK0011" 退货取消订购金额
      FROM ITF_SAP.SAP_BIC_AZTCRD00100 A,
           ITF_SAP.SAP_BIC_PZMATERIAL  B,
           ITF_SAP.SAP_BIC_TZMATERIAL  C,
           ITF_SAP."/BIC/OHZO_MSCODE"  D,
           ITF_SAP."/BIC/OHZO_MSGB"    E,
           ITF_SAP."/BIC/OHZO_PZHEN"   H,
           ITF_SAP."/BIC/OHZO_TSHENG"  H1
     WHERE A."/BIC/ZTCMC021" BETWEEN '20060301' AND InStrDate
       AND NVL(A."/BIC/ZCRMD001", ' ') <> 'TA10' --剔除预订购
       AND NVL(A."/BIC/ZCRMD048", ' ') <> 'TANN' --剔除赠品
       AND A."/BIC/ZCRMD199" IS NULL --剔除预订购
       AND A."/BIC/ZMATERIAL" = B."/BIC/ZMATERIAL"(+)
       AND A."/BIC/ZMATERIAL" = C."/BIC/ZMATERIAL"(+)
       AND A."/BIC/ZKUNNR_L1" = D."/BIC/ZKUNNR_L1"(+)
       AND D.LANGU(+) = '1'
       AND A."/BIC/ZKUNNR_L2" = E."/BIC/ZKUNNR_L2"(+)
       AND E.LANGU(+) = '1'
       AND A."/BIC/ZTCMC020" = H."/BIC/ZTCMC020"(+)
       AND H."/BIC/ZTCMC017" = H1."/BIC/ZTCMC017"(+);
  --按天统计临时表
  INSERT INTO DATA_SALE_DAY_TP
    SELECT TO_CHAR(T.P_DATE, 'YYYYMMDD') P_DATE,
           T.ORDER_CHANNEL_L1,
           T.CHANNEL_L1_NAME,
           T.ORDER_CHANNEL_L2,
           T.CHANNEL_L2_NAME,
           T.CUST_GRADE,
           LTRIM(T.CATE_B, '0') CATE_B,
           T.PROVINCE,
           T.PROVINCE_NAME,
           SUM(T.TOT_ORDER_QTY) TOT_ORDER_QTY,
           SUM(T.TOT_ORDER_AMT) TOT_ORDER_AMT,
           SUM(T.TOT_ORDER_COST) TOT_ORDER_COST,
           SUM(T.NET_ORDER_QTY) NET_ORDER_QTY,
           SUM(T.NET_ORDER_AMT) NET_ORDER_AMT,
           SUM(T.NET_ORDER_COST) NET_ORDER_COST,
           SUM(T.EFFECT_ORDER_QTY) EFFECT_ORDER_QTY,
           SUM(T.EFFECT_ORDER_AMT) EFFECT_ORDER_AMT,
           SUM(T.EFFECT_PRICE_AMT) EFFECT_PRICE_AMT,
           SUM(T.EFFECT_ORDER_COST) EFFECT_ORDER_COST,
           SUM(T.EFFECT_DISCOUNT_AMT) EFFECT_DISCOUNT_AMT,
           SUM(T.ORDER_CANCEL_QTY) ORDER_CANCEL_QTY,
           SUM(T.ORDER_CANCEL_AMT) ORDER_CANCEL_AMT,
           SUM(T.ORDER_REJECT_QTY) ORDER_REJECT_QTY,
           SUM(T.ORDER_REJECT_AMT) ORDER_REJECT_AMT,
           SUM(T.ORDER_REFUND_QTY) ORDER_REFUND_QTY,
           SUM(T.ORDER_REFUND_AMT) ORDER_REFUND_AMT,
           SUM(T.ORDER_REJECT_CANCEL_QTY) ORDER_REJECT_CANCEL_QTY,
           SUM(T.ORDER_REJECT_CANCEL_AMT) ORDER_REJECT_CANCEL_AMT,
           SUM(T.ORDER_REFUND_CANCEL_QTY) ORDER_REFUND_CANCEL_QTY,
           SUM(T.ORDER_REFUND_CANCEL_AMT) ORDER_REFUND_CANCEL_AMT
      FROM DATA_SALE_DAY_ITEM T
     GROUP BY TO_CHAR(T.P_DATE, 'YYYYMMDD'),
              T.ORDER_CHANNEL_L1,
              T.CHANNEL_L1_NAME,
              T.ORDER_CHANNEL_L2,
              T.CHANNEL_L2_NAME,
              T.CUST_GRADE,
              LTRIM(T.CATE_B, '0'),
              T.PROVINCE,
              T.PROVINCE_NAME;
  --按天统计数据表
  INSERT INTO DATA_SALE_DAY
    SELECT A.*,
           B.TOT_ORDER_QTY           TOT_ORDER_QTY_L1,
           B.TOT_ORDER_AMT           TOT_ORDER_AMT_L1,
           B.TOT_ORDER_COST          TOT_ORDER_COST_L1,
           B.NET_ORDER_QTY           NET_ORDER_QTY_L1,
           B.NET_ORDER_AMT           NET_ORDER_AMT_L1,
           B.NET_ORDER_COST          NET_ORDER_COST_L1,
           B.EFFECT_ORDER_QTY        EFFECT_ORDER_QTY_L1,
           B.EFFECT_ORDER_AMT        EFFECT_ORDER_AMT_L1,
           B.EFFECT_PRICE_AMT        EFFECT_PRICE_AMT_L1,
           B.EFFECT_ORDER_COST       EFFECT_ORDER_COST_L1,
           B.EFFECT_DISCOUNT_AMT     EFFECT_DISCOUNT_AMT_L1,
           B.ORDER_CANCEL_QTY        ORDER_CANCEL_QTY_L1,
           B.ORDER_CANCEL_AMT        ORDER_CANCEL_AMT_L1,
           B.ORDER_REJECT_QTY        ORDER_REJECT_QTY_L1,
           B.ORDER_REJECT_AMT        ORDER_REJECT_AMT_L1,
           B.ORDER_REFUND_QTY        ORDER_REFUND_QTY_L1,
           B.ORDER_REFUND_AMT        ORDER_REFUND_AMT_L1,
           B.ORDER_REJECT_CANCEL_QTY ORDER_REJECT_CANCEL_QTY_L1,
           B.ORDER_REJECT_CANCEL_AMT ORDER_REJECT_CANCEL_AMT_L1,
           B.ORDER_REFUND_CANCEL_QTY ORDER_REFUND_CANCEL_QTY_L1,
           B.ORDER_REFUND_CANCEL_AMT ORDER_REFUND_CANCEL_AMT_L1
      FROM DATA_SALE_DAY_TP A, DATA_SALE_DAY_TP B
     WHERE ((SUBSTR(A.P_DATE, 1, 4) - 1) || SUBSTR(A.P_DATE, 5, 4)) =
           B.P_DATE(+)
       AND A.ORDER_CHANNEL_L1 = B.ORDER_CHANNEL_L1(+)
       AND A.CHANNEL_L1_NAME = B.CHANNEL_L1_NAME(+)
       AND A.ORDER_CHANNEL_L2 = B.ORDER_CHANNEL_L2(+)
       AND A.CHANNEL_L2_NAME = B.CHANNEL_L2_NAME(+)
       AND A.CUST_GRADE = B.CUST_GRADE(+)
       AND A.CATE_B = B.CATE_B(+)
       AND A.PROVINCE = B.PROVINCE(+)
       AND A.PROVINCE_NAME = B.PROVINCE_NAME(+);
  --按月统计临时表
  INSERT INTO DATA_SALE_MONTH_TP
    SELECT SUBSTR(A.P_DATE, 1, 6) P_MONTH,
           A.ORDER_CHANNEL_L1,
           A.CHANNEL_L1_NAME,
           A.ORDER_CHANNEL_L2,
           A.CHANNEL_L2_NAME,
           A.CUST_GRADE,
           A.CATE_B,
           A.PROVINCE,
           A.PROVINCE_NAME,
           SUM(TOT_ORDER_QTY) TOT_ORDER_QTY,
           SUM(TOT_ORDER_AMT) TOT_ORDER_AMT,
           SUM(TOT_ORDER_COST) TOT_ORDER_COST,
           SUM(NET_ORDER_QTY) NET_ORDER_QTY,
           SUM(NET_ORDER_AMT) NET_ORDER_AMT,
           SUM(NET_ORDER_COST) NET_ORDER_COST,
           SUM(EFFECT_ORDER_QTY) EFFECT_ORDER_QTY,
           SUM(EFFECT_ORDER_AMT) EFFECT_ORDER_AMT,
           SUM(EFFECT_PRICE_AMT) EFFECT_PRICE_AMT,
           SUM(EFFECT_ORDER_COST) EFFECT_ORDER_COST,
           SUM(EFFECT_DISCOUNT_AMT) EFFECT_DISCOUNT_AMT,
           SUM(ORDER_CANCEL_QTY) ORDER_CANCEL_QTY,
           SUM(ORDER_CANCEL_AMT) ORDER_CANCEL_AMT,
           SUM(ORDER_REJECT_QTY) ORDER_REJECT_QTY,
           SUM(ORDER_REJECT_AMT) ORDER_REJECT_AMT,
           SUM(ORDER_REFUND_QTY) ORDER_REFUND_QTY,
           SUM(ORDER_REFUND_AMT) ORDER_REFUND_AMT,
           SUM(ORDER_REJECT_CANCEL_QTY) ORDER_REJECT_CANCEL_QTY,
           SUM(ORDER_REJECT_CANCEL_AMT) ORDER_REJECT_CANCEL_AMT,
           SUM(ORDER_REFUND_CANCEL_QTY) ORDER_REFUND_CANCEL_QTY,
           SUM(ORDER_REFUND_CANCEL_AMT) ORDER_REFUND_CANCEL_AMT,
           SUM(TOT_ORDER_QTY) / B.DATE_CNT TOT_ORDER_QTY_AVG,
           SUM(TOT_ORDER_AMT) / B.DATE_CNT TOT_ORDER_AMT_AVG,
           SUM(TOT_ORDER_COST) / B.DATE_CNT TOT_ORDER_COST_AVG,
           SUM(NET_ORDER_QTY) / B.DATE_CNT NET_ORDER_QTY_AVG,
           SUM(NET_ORDER_AMT) / B.DATE_CNT NET_ORDER_AMT_AVG,
           SUM(NET_ORDER_COST) / B.DATE_CNT NET_ORDER_COST_AVG,
           SUM(EFFECT_ORDER_QTY) / B.DATE_CNT EFFECT_ORDER_QTY_AVG,
           SUM(EFFECT_ORDER_AMT) / B.DATE_CNT EFFECT_ORDER_AMT_AVG,
           SUM(EFFECT_PRICE_AMT) / B.DATE_CNT EFFECT_PRICE_AMT_AVG,
           SUM(EFFECT_ORDER_COST) / B.DATE_CNT EFFECT_ORDER_COST_AVG,
           SUM(EFFECT_DISCOUNT_AMT) / B.DATE_CNT EFFECT_DISCOUNT_AMT_AVG,
           SUM(ORDER_CANCEL_QTY) / B.DATE_CNT ORDER_CANCEL_QTY_AVG,
           SUM(ORDER_CANCEL_AMT) / B.DATE_CNT ORDER_CANCEL_AMT_AVG,
           SUM(ORDER_REJECT_QTY) / B.DATE_CNT ORDER_REJECT_QTY_AVG,
           SUM(ORDER_REJECT_AMT) / B.DATE_CNT ORDER_REJECT_AMT_AVG,
           SUM(ORDER_REFUND_QTY) / B.DATE_CNT ORDER_REFUND_QTY_AVG,
           SUM(ORDER_REFUND_AMT) / B.DATE_CNT ORDER_REFUND_AMT_AVG,
           SUM(ORDER_REJECT_CANCEL_QTY) / B.DATE_CNT ORDER_REJECT_CANCEL_QTY_AVG,
           SUM(ORDER_REJECT_CANCEL_AMT) / B.DATE_CNT ORDER_REJECT_CANCEL_AMT_AVG,
           SUM(ORDER_REFUND_CANCEL_QTY) / B.DATE_CNT ORDER_REFUND_CANCEL_QTY_AVG,
           SUM(ORDER_REFUND_CANCEL_AMT) / B.DATE_CNT ORDER_REFUND_CANCEL_AMT_AVG
      FROM DATA_SALE_DAY_TP A,
           (SELECT SUBSTR(P_DATE, 1, 6) P_MONTH,
                   COUNT(DISTINCT SUBSTR(P_DATE, 7, 2)) DATE_CNT
              FROM DATA_SALE_DAY_TP
             GROUP BY SUBSTR(P_DATE, 1, 6)) B
     WHERE SUBSTR(A.P_DATE, 1, 6) = B.P_MONTH(+)
     GROUP BY SUBSTR(A.P_DATE, 1, 6),
              A.ORDER_CHANNEL_L1,
              A.CHANNEL_L1_NAME,
              A.ORDER_CHANNEL_L2,
              A.CHANNEL_L2_NAME,
              A.CUST_GRADE,
              A.CATE_B,
              A.PROVINCE,
              A.PROVINCE_NAME,
              B.DATE_CNT;
  --按月统计数据表
  INSERT INTO DATA_SALE_MONTH
    SELECT A.*,
           B.TOT_ORDER_QTY               TOT_ORDER_QTY_L1,
           B.TOT_ORDER_AMT               TOT_ORDER_AMT_L1,
           B.TOT_ORDER_COST              TOT_ORDER_COST_L1,
           B.NET_ORDER_QTY               NET_ORDER_QTY_L1,
           B.NET_ORDER_AMT               NET_ORDER_AMT_L1,
           B.NET_ORDER_COST              NET_ORDER_COST_L1,
           B.EFFECT_ORDER_QTY            EFFECT_ORDER_QTY_L1,
           B.EFFECT_ORDER_AMT            EFFECT_ORDER_AMT_L1,
           B.EFFECT_PRICE_AMT            EFFECT_PRICE_AMT_L1,
           B.EFFECT_ORDER_COST           EFFECT_ORDER_COST_L1,
           B.EFFECT_DISCOUNT_AMT         EFFECT_DISCOUNT_AMT_L1,
           B.ORDER_CANCEL_QTY            ORDER_CANCEL_QTY_L1,
           B.ORDER_CANCEL_AMT            ORDER_CANCEL_AMT_L1,
           B.ORDER_REJECT_QTY            ORDER_REJECT_QTY_L1,
           B.ORDER_REJECT_AMT            ORDER_REJECT_AMT_L1,
           B.ORDER_REFUND_QTY            ORDER_REFUND_QTY_L1,
           B.ORDER_REFUND_AMT            ORDER_REFUND_AMT_L1,
           B.ORDER_REJECT_CANCEL_QTY     ORDER_REJECT_CANCEL_QTY_L1,
           B.ORDER_REJECT_CANCEL_AMT     ORDER_REJECT_CANCEL_AMT_L1,
           B.ORDER_REFUND_CANCEL_QTY     ORDER_REFUND_CANCEL_QTY_L1,
           B.ORDER_REFUND_CANCEL_AMT     ORDER_REFUND_CANCEL_AMT_L1,
           B.TOT_ORDER_QTY_AVG           TOT_ORDER_QTY_AVG_L1,
           B.TOT_ORDER_AMT_AVG           TOT_ORDER_AMT_AVG_L1,
           B.TOT_ORDER_COST_AVG          TOT_ORDER_COST_AVG_L1,
           B.NET_ORDER_QTY_AVG           NET_ORDER_QTY_AVG_L1,
           B.NET_ORDER_AMT_AVG           NET_ORDER_AMT_AVG_L1,
           B.NET_ORDER_COST_AVG          NET_ORDER_COST_AVG_L1,
           B.EFFECT_ORDER_QTY_AVG        EFFECT_ORDER_QTY_AVG_L1,
           B.EFFECT_ORDER_AMT_AVG        EFFECT_ORDER_AMT_AVG_L1,
           B.EFFECT_PRICE_AMT_AVG        EFFECT_PRICE_AMT_AVG_L1,
           B.EFFECT_ORDER_COST_AVG       EFFECT_ORDER_COST_AVG_L1,
           B.EFFECT_DISCOUNT_AMT_AVG     EFFECT_DISCOUNT_AMT_AVG_L1,
           B.ORDER_CANCEL_QTY_AVG        ORDER_CANCEL_QTY_AVG_L1,
           B.ORDER_CANCEL_AMT_AVG        ORDER_CANCEL_AMT_AVG_L1,
           B.ORDER_REJECT_QTY_AVG        ORDER_REJECT_QTY_AVG_L1,
           B.ORDER_REJECT_AMT_AVG        ORDER_REJECT_AMT_AVG_L1,
           B.ORDER_REFUND_QTY_AVG        ORDER_REFUND_QTY_AVG_L1,
           B.ORDER_REFUND_AMT_AVG        ORDER_REFUND_AMT_AVG_L1,
           B.ORDER_REJECT_CANCEL_QTY_AVG ORDER_REJECT_CANCEL_QTY_AVG_L1,
           B.ORDER_REJECT_CANCEL_AMT_AVG ORDER_REJECT_CANCEL_AMT_AVG_L1,
           B.ORDER_REFUND_CANCEL_QTY_AVG ORDER_REFUND_CANCEL_QTY_AVG_L1,
           B.ORDER_REFUND_CANCEL_AMT_AVG ORDER_REFUND_CANCEL_AMT_AVG_L1,
           C.TOT_ORDER_QTY               TOT_ORDER_QTY_L2,
           C.TOT_ORDER_AMT               TOT_ORDER_AMT_L2,
           C.TOT_ORDER_COST              TOT_ORDER_COST_L2,
           C.NET_ORDER_QTY               NET_ORDER_QTY_L2,
           C.NET_ORDER_AMT               NET_ORDER_AMT_L2,
           C.NET_ORDER_COST              NET_ORDER_COST_L2,
           C.EFFECT_ORDER_QTY            EFFECT_ORDER_QTY_L2,
           C.EFFECT_ORDER_AMT            EFFECT_ORDER_AMT_L2,
           C.EFFECT_PRICE_AMT            EFFECT_PRICE_AMT_L2,
           C.EFFECT_ORDER_COST           EFFECT_ORDER_COST_L2,
           C.EFFECT_DISCOUNT_AMT         EFFECT_DISCOUNT_AMT_L2,
           C.ORDER_CANCEL_QTY            ORDER_CANCEL_QTY_L2,
           C.ORDER_CANCEL_AMT            ORDER_CANCEL_AMT_L2,
           C.ORDER_REJECT_QTY            ORDER_REJECT_QTY_L2,
           C.ORDER_REJECT_AMT            ORDER_REJECT_AMT_L2,
           C.ORDER_REFUND_QTY            ORDER_REFUND_QTY_L2,
           C.ORDER_REFUND_AMT            ORDER_REFUND_AMT_L2,
           C.ORDER_REJECT_CANCEL_QTY     ORDER_REJECT_CANCEL_QTY_L2,
           C.ORDER_REJECT_CANCEL_AMT     ORDER_REJECT_CANCEL_AMT_L2,
           C.ORDER_REFUND_CANCEL_QTY     ORDER_REFUND_CANCEL_QTY_L2,
           C.ORDER_REFUND_CANCEL_AMT     ORDER_REFUND_CANCEL_AMT_L2,
           C.TOT_ORDER_QTY_AVG           TOT_ORDER_QTY_AVG_L2,
           C.TOT_ORDER_AMT_AVG           TOT_ORDER_AMT_AVG_L2,
           C.TOT_ORDER_COST_AVG          TOT_ORDER_COST_AVG_L2,
           C.NET_ORDER_QTY_AVG           NET_ORDER_QTY_AVG_L2,
           C.NET_ORDER_AMT_AVG           NET_ORDER_AMT_AVG_L2,
           C.NET_ORDER_COST_AVG          NET_ORDER_COST_AVG_L2,
           C.EFFECT_ORDER_QTY_AVG        EFFECT_ORDER_QTY_AVG_L2,
           C.EFFECT_ORDER_AMT_AVG        EFFECT_ORDER_AMT_AVG_L2,
           C.EFFECT_PRICE_AMT_AVG        EFFECT_PRICE_AMT_AVG_L2,
           C.EFFECT_ORDER_COST_AVG       EFFECT_ORDER_COST_AVG_L2,
           C.EFFECT_DISCOUNT_AMT_AVG     EFFECT_DISCOUNT_AMT_AVG_L2,
           C.ORDER_CANCEL_QTY_AVG        ORDER_CANCEL_QTY_AVG_L2,
           C.ORDER_CANCEL_AMT_AVG        ORDER_CANCEL_AMT_AVG_L2,
           C.ORDER_REJECT_QTY_AVG        ORDER_REJECT_QTY_AVG_L2,
           C.ORDER_REJECT_AMT_AVG        ORDER_REJECT_AMT_AVG_L2,
           C.ORDER_REFUND_QTY_AVG        ORDER_REFUND_QTY_AVG_L2,
           C.ORDER_REFUND_AMT_AVG        ORDER_REFUND_AMT_AVG_L2,
           C.ORDER_REJECT_CANCEL_QTY_AVG ORDER_REJECT_CANCEL_QTY_AVG_L2,
           C.ORDER_REJECT_CANCEL_AMT_AVG ORDER_REJECT_CANCEL_AMT_AVG_L2,
           C.ORDER_REFUND_CANCEL_QTY_AVG ORDER_REFUND_CANCEL_QTY_AVG_L2,
           C.ORDER_REFUND_CANCEL_AMT_AVG ORDER_REFUND_CANCEL_AMT_AVG_L2
      FROM DATA_SALE_MONTH_TP A, DATA_SALE_MONTH_TP B, DATA_SALE_MONTH_TP C
     WHERE ((SUBSTR(A.P_MONTH, 1, 4) - 1) || SUBSTR(A.P_MONTH, 5, 2)) =
           B.P_MONTH(+)
       AND A.ORDER_CHANNEL_L1 = B.ORDER_CHANNEL_L1(+)
       AND A.CHANNEL_L1_NAME = B.CHANNEL_L1_NAME(+)
       AND A.ORDER_CHANNEL_L2 = B.ORDER_CHANNEL_L2(+)
       AND A.CHANNEL_L2_NAME = B.CHANNEL_L2_NAME(+)
       AND A.CUST_GRADE = B.CUST_GRADE(+)
       AND A.CATE_B = B.CATE_B(+)
       AND A.PROVINCE = B.PROVINCE(+)
       AND A.PROVINCE_NAME = B.PROVINCE_NAME(+)
       AND TO_CHAR(ADD_MONTHS(TO_DATE(A.P_MONTH, 'YYYYMM'), -1), 'YYYYMM') =
           C.P_MONTH(+)
       AND A.ORDER_CHANNEL_L1 = C.ORDER_CHANNEL_L1(+)
       AND A.CHANNEL_L1_NAME = C.CHANNEL_L1_NAME(+)
       AND A.ORDER_CHANNEL_L2 = C.ORDER_CHANNEL_L2(+)
       AND A.CHANNEL_L2_NAME = C.CHANNEL_L2_NAME(+)
       AND A.CUST_GRADE = C.CUST_GRADE(+)
       AND A.CATE_B = C.CATE_B(+)
       AND A.PROVINCE = C.PROVINCE(+)
       AND A.PROVINCE_NAME = C.PROVINCE_NAME(+);

COMMIT;

exception
  when others then
    os_err_msg := to_char('调用失败,请检查! ' || sqlerrm);
    rollback;
    return;

END sp_cust_main_data_yx;

ORACLE no1 存储过程插入更新表数据的更多相关文章

  1. MYSQL 使用存储过程批量更新表数据

    功能:实现将表result_good_city_dzl中的字段lat更新为表 result_good_city_lh中的lat,条件是两个表中的id一样 即: update result_good_c ...

  2. oracle插入,更新,删除数据

    插入,更新,删除数据 oracle提供了功能丰富的数据库管理语句 包括有效的向数据库中插入数据的insert语句 更新数据的update语句 以及当数据不再使用时删除数据的delete语句 更改数据之 ...

  3. 在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据)

    原文:在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所 ...

  4. Oracle数据库中插入日期型数据(to_date的用法)(转载)

    往Oracle数据库中插入日期型数据(to_date的用法) INSERT  INTO  FLOOR  VALUES  ( to_date ( '2007-12-20 18:31:34' , 'YYY ...

  5. SQL语句往Oracle数据库中插入日期型数据(to_date的用法)

    Oracle 在操作数据库上相比于其他的 T-sql 有微小的差别,但是在插入时间类型的数据是必须要注意他的 to_date 方法,具体的情况如下: --SQL语句往Oracle数据库中插入日期型数据 ...

  6. Oracle与SQL Server实现表数据同步

    将SQLServer2008中的某些表同步到Oracle数据库中,不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现. 实例1:SQLServer2008有一个表employ_ ...

  7. oracle insert into 插入多组数据方法总结

    网上好多oracle 的文章,多是以oracle开头,内容确实其他sql,一幅气死人不偿命的嘴脸着实让人难受. 今天就更新点oracle 使用insert into插入数据的方式: 1.oracle ...

  8. 一次EF批量插入多表数据的性能优化经历

    距离上次的博客已经有15个多月了,感慨有些事情还是需要坚持,一旦停下来很有可能就会停很久或者从此再也不会坚持.但我个人一直还坚持认为属于技术狂热份子,且喜欢精益求精的那种.最近遇到两个和数据迁移相关的 ...

  9. Oracle 跨库 查询 复制表数据 分布式查询

    方法一: 在眼下绝大部分数据库有分布式查询的须要.以下简单的介绍怎样在oracle中配置实现跨库訪问. 比方如今有2个数据库服务器,安装了2个数据库.数据库server A和B.如今来实如今A库中訪问 ...

随机推荐

  1. CefSharp中实现Chrome中jS导出Excel

    [前言] 在博客园闲逛了一年多,平时都是借鉴别人的成功经验,总觉得自己应该分享点什么,但是苦于自己技术有限,平时又不爱写东西,所以一直没有写过任何东西.毕业一年多,在现实工作中遇到各种问题,深切体会到 ...

  2. FZU Problem 2125 简单的等式

    思路:x绝对小于根号n,再由s(x,m)可以缩小范围.1e9十六进制大约算出每位和相加100左右.这种题直接判断范围再暴力. #include<stdio.h> #include<s ...

  3. 非阻塞,send后马上close消息能成功发出去吗

    send返回成功的话就能. 判断send返回,假如返回的值是成功,则说明要发送的放到了tcp的发送缓冲区内了,然后在close的话等于再放了一个关闭信号到缓冲区里的. 由于TCP是有序的,假如对端此时 ...

  4. Docker系列(四)Dockerfile

    基本语法 使用 #来注释 FROM 指令告诉Docker使用哪个镜像作为基础 接着是维护者信息 RUN 开头的指令会在创建中运行,比如安装软件包 FROM 格式: FROM<image>或 ...

  5. Storm系列(十)聚流示例

    功能:将多个数据源的数据汇集到一个处理单元进行集中分类处理: 入口类TestMain 1  ; i < size; i++) { 31              content += input ...

  6. vmware虚拟机下ubuntu 13.04使用zeranoe脚本交叉编译ffmpeg

    2013-07-01今天是建党节,习总书记指出,党的建设要以“照镜子.正衣冠.洗洗澡.治治病”为总要求.希望我们的党越来越纯洁,为人民谋福利.言归正传,每次项目中需要编译相应的ffmpeg,都很费时费 ...

  7. winform窗体跟随窗体

           Form2 frm2 = new Form2();         private void MoveProc()         {             frm2.StartPos ...

  8. [Javascript] Drawing Paths - Lines and Rectangles

    <!DOCTYPE html> <html> <head> <meta name="description" content=" ...

  9. IAP升级功能编写初期的一些困惑与疑问---完毕功能后的总结

    IAP的源代码等资料我上传了,压缩包内有12个文件,,http://download.csdn.net/detail/f907279313/7524849(要积分的辛苦收集的你们就给点积分吧) 还有还 ...

  10. Android Studio中Gradle使用详解

    一)基本配置 build配置 buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools. ...