Oracle 销售统计报表 实例

待优化的SQL

create or replace procedure slbksoiqa2(
     var_date1    date default to_date(‘1969-01-01‘,‘yyyy-mm-dd‘),
     var_date2    date default to_date(‘2069-01-01‘,‘yyyy-mm-dd‘),
     var_cust1    varchar2 default ‘‘,
     var_cust2    varchar2 default ‘ZZZZZZZZ‘,
     var_slspsn1  varchar2 default ‘‘,
     var_slspsn2  varchar2 default ‘ZZZZZZZZ‘
     --resultcursor   out   sys_refcursor
)
/*
作者:苏炎江
时间:2016-10-18
功能:销售预留费用统计(新)
内容:统计开模费均摊、认证费均摊、制定运费、市场费均摊、销售折扣加价、rom备件预留金、差异总额
*/
as
    -------已下单金额--------
    var_diff_amt     decimal(19,4);   --下单
    var_list_amt     decimal(19,4);
    var_freight_amt  decimal(19,4);
    var_mould_amt    decimal(19,4);
    var_attest_amt   decimal(19,4);
    var_market_amt   decimal(19,4);
    var_rom_amt      decimal(19,4);
    var_tool_amt     decimal(19,4);
    var_cust         varchar2(50);
    var_slspsn       varchar2(50);
    var_count        Integer;
    -------ROM/DMZ 预留分配--------
    var_list_amt1    decimal(19,4);
    var_freight_amt1 decimal(19,4);
    var_mould_amt1   decimal(19,4);
    var_attest_amt1  decimal(19,4);
    var_market_amt1  decimal(19,4);
    var_tool_amt1    decimal(19,4);
   /* --------期初金额----------
    var_list_amt2    decimal(19,4);
    var_freight_amt2 decimal(19,4);
    var_mould_amt2   decimal(19,4);
    var_attest_amt2  decimal(19,4);
    var_market_amt2  decimal(19,4);
    var_rom_amt2     decimal(19,4);
    var_diff_amt2    decimal(19,4);*/
begin

    delete from tpt_slbkso1;
    insert into tpt_slbkso1(tpt_cust,tpt_slspsn,
                           tpt_sod_list_amt,tpt_sod_freight_amt,tpt_sod_mould_amt,tpt_sod_attest_amt,tpt_sod_rom_amt,tpt_sod_market_amt,tpt_sod_diff_amt,tpt_sod_tool_amt ,
                           tpt_mrbd_list_amt,tpt_mrbd_freight_amt,tpt_mrbd_mould_amt,tpt_mrbd_attest_amt,tpt_mrbd_rom_amt, tpt_mrbd_market_amt,tpt_mrbd_diff_amt,tpt_mrb_tool_amt ,
                           tpt_sod_list_amt1,tpt_sod_freight_amt1,tpt_sod_mould_amt1,tpt_sod_attest_amt1,tpt_sod_rom_amt1,tpt_sod_market_amt1,tpt_sod_diff_amt1,tpt_sod_tool_amt1
                           )

    select sdh_cust,sdh_so_slspsn,
           sum(sod_list_amt),sum(sod_freight_amt),sum(sod_mould_amt),sum(sod_attest_amt),sum(sod_rom_amt),sum(sod_market_amt),sum(sod_diff_amt),sum(sod_tool_amt),
           sum(mrbd_list_amt),sum(mrbd_freight_amt),sum(mrbd_mould_amt),sum(mrbd_attest_amt),sum(mrbd_rom_atm),sum(mrbd_market_amt),sum(mrbd_diff_amt),sum(mrbd_tool_amt),
           sum(sod_list_amt1),sum(sod_freight_amt1),sum(sod_mould_amt1),sum(sod_attest_amt1),sum(sod_rom_amt1),sum(sod_market_amt1),sum(sod_diff_amt1),sum(sod_tool_amt1)
    from(
     ------------------------------------已关单出货数量----------------------------------------------------
      select sdh_cust,sdh_so_slspsn,
             nvl(sod_list_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_list_amt,nvl(sod_freight_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_freight_amt,
             nvl(sod_mould_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_mould_amt,nvl(sod_attest_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_attest_amt,
             nvl(sod_rom_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_rom_amt,nvl(sod_market_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_market_amt,
             nvl(get_so_nbr_price(sdh_dn_dn,sdh_dnd_line) * sdh_qty_shp * sdh_ex_rate,0) as sod_diff_amt,nvl(sod_tool_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_tool_amt,

             0 as mrbd_list_amt,0 as mrbd_freight_amt,0 as mrbd_mould_amt,0 as mrbd_attest_amt,0 as mrbd_rom_atm,0 as mrbd_market_amt,0 as mrbd_diff_amt,0 as mrbd_tool_amt,

             0 as sod_list_amt1,0 as sod_freight_amt1,0 as sod_mould_amt1,0 as sod_attest_amt1,0 as sod_rom_amt1,0 as sod_market_amt1,0 as sod_diff_amt1,0 sod_tool_amt1
      from sdh_hist
      left join sod_det on sod_nbr=sdh_so_nbr and sod_line=sdh_sod_line
      left join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from so_mstr) on so_nbr=sdh_so_nbr and so_char1 is null
      left join gend_det on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join exd_det on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where between2(sdh_cust,var_cust1,var_cust2)=1
        and between2(sdh_so_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(sdh_crt_date,‘dd‘)>=var_date1 and trunc(sdh_crt_date,‘dd‘)<=var_date2
        and trunc(so_ord_date,‘dd‘) >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and sdh_qty_shp <> 0
        and so_char1 is null
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and sdh_prog_code  in (‘SLDNMTA1‘,‘SLDNMTA5‘) --and sod_status=‘C‘   --出货数量含关单与未关单的出货数量
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
      union all
      select sdh_cust,sdh_so_slspsn,nvl(sod_list_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_list_amt,nvl(sod_freight_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_freight_amt,
             nvl(sod_mould_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_mould_amt,nvl(sod_attest_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_attest_amt,
             nvl(sod_rom_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_rom_amt,nvl(sod_market_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_market_amt,
             nvl([email protected]inanerp(sdh_dn_dn,sdh_dnd_line) * sdh_qty_shp * sdh_ex_rate,0) as sod_diff_amt,
             nvl(sod_tool_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_tool_amt,
             0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0
      from [email protected]
      left join [email protected] on sod_nbr=sdh_so_nbr and sod_line=sdh_sod_line
      left join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from [email protected]) on so_nbr=sdh_so_nbr and so_char1 is null
      left join [email protected] on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join [email protected] on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where between2(sdh_cust,var_cust1,var_cust2)=1
        and between2(sdh_so_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(sdh_crt_date,‘dd‘)>=var_date1 and trunc(sdh_crt_date,‘dd‘)<=var_date2
        and trunc(so_ord_date,‘dd‘) >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and sdh_qty_shp <> 0
        and so_char1 is null
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and sdh_prog_code  in (‘SLDNMTA1‘,‘SLDNMTA5‘) --and sod_status=‘C‘    --出货数量含关单与未关单的出货数量
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
     ---------------------------------------------------已关单退货------------------------------------------------------
     union all
     select sdh_cust,sdh_so_slspsn,
             nvl(sod_list_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_list_amt,nvl(sod_freight_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_freight_amt,
             nvl(sod_mould_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_mould_amt,nvl(sod_attest_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_attest_amt,
             nvl(sod_rom_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_rom_amt,nvl(sod_market_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_market_amt,
             nvl(get_so_nbr_price1(sdh_dn_dn,sdh_dnd_line) * sdh_qty_shp * sdh_ex_rate,0) as sod_diff_amt,nvl(sod_tool_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_tool_amt,

             0 as mrbd_list_amt,0 as mrbd_freight_amt,0 as mrbd_mould_amt,0 as mrbd_attest_amt,0 as mrbd_rom_atm,0 as mrbd_market_amt,0 as mrbd_diff_amt,0 as mrbd_tool_amt,

             0 as sod_list_amt1,0 as sod_freight_amt1,0 as sod_mould_amt1,0 as sod_attest_amt1,0 as sod_rom_amt1,0 as sod_market_amt1,0 as sod_diff_amt1,0 sod_tool_amt1
      from sdh_hist
      left join sod_det on sod_nbr=sdh_so_nbr and sod_line=sdh_sod_line
      left join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from so_mstr) on so_nbr=sdh_so_nbr and so_char1 is null
      left join gend_det on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join exd_det on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where between2(sdh_cust,var_cust1,var_cust2)=1
        and between2(sdh_so_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(sdh_crt_date,‘dd‘)>=var_date1 and trunc(sdh_crt_date,‘dd‘)<=var_date2
        and trunc(so_ord_date,‘dd‘) >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and sdh_qty_shp <> 0
        and so_char1 is null
        --and sod_status = ‘C‘
        and sdh_prog_code  in(‘SLCRMTA1‘,‘SLCRMTA2‘)
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
      union all
      select sdh_cust,sdh_so_slspsn,nvl(sod_list_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_list_amt,nvl(sod_freight_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_freight_amt,
             nvl(sod_mould_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_mould_amt,nvl(sod_attest_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_attest_amt,
             nvl(sod_rom_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_rom_amt,nvl(sod_market_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_market_amt,
             nvl([email protected](sdh_dn_dn,sdh_dnd_line) * sdh_qty_shp * sdh_ex_rate,0) as sod_diff_amt,
             nvl(sod_tool_cost*sdh_qty_shp * sdh_ex_rate*nvl(exd_r_rate,1),0) as sod_tool_amt,
             0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0
      from [email protected]
      left join [email protected] on sod_nbr=sdh_so_nbr and sod_line=sdh_sod_line
      left join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from [email protected]) on so_nbr=sdh_so_nbr and so_char1 is null
      left join [email protected] on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join [email protected] on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where between2(sdh_cust,var_cust1,var_cust2)=1
        and between2(sdh_so_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(sdh_crt_date,‘dd‘)>=var_date1 and trunc(sdh_crt_date,‘dd‘)<=var_date2
        and trunc(so_ord_date,‘dd‘) >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and sdh_qty_shp <> 0
        and so_char1 is null
        --and sod_status = ‘C‘
        and sdh_prog_code  in (‘SLCRMTA1‘,‘SLCRMTA2‘)
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
     -------------------------------------未关单 未出货数量---------------------------------------------------------------------
      union all
      select so_cust,so_slspsn,
             0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,
             nvl(sod_list_cost*(sod_qty_ord-sod_qty_shp)*so_ex_rate*nvl(exd_r_rate,1),0),nvl(sod_freight_cost*(sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),
             nvl(sod_mould_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),nvl(sod_attest_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),
             nvl(sod_rom_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),nvl(sod_market_cost*(sod_qty_ord-sod_qty_shp) * so_ex_rate*nvl(exd_r_rate,1),0),
             nvl(round((sod_adj_price+sod_sp_adj_price+sod_top_adj_price) * (sod_qty_ord-sod_qty_shp),4) * so_ex_rate,0),nvl(sod_tool_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0)
      from sod_det
      left join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from so_mstr) on so_nbr=sod_nbr  and so_char1 is null
      left join gend_det on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join exd_det on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where between2(so_cust,var_cust1,var_cust2)=1
        and between2(so_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(so_ord_date,‘dd‘)>=var_date1
        and trunc(so_ord_date,‘dd‘)<=var_date2
        and trunc(so_ord_date,‘dd‘) >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and so_char1 is null
        and sod_status is null
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
     union all
      select so_cust,so_slspsn,
             0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,
             nvl(sod_list_cost*(sod_qty_ord-sod_qty_shp)*so_ex_rate*nvl(exd_r_rate,1),0),nvl(sod_freight_cost*(sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),
             nvl(sod_mould_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),nvl(sod_attest_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),
             nvl(sod_rom_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0),nvl(sod_market_cost*(sod_qty_ord-sod_qty_shp) * so_ex_rate*nvl(exd_r_rate,1),0),
             nvl(round((sod_adj_price+sod_sp_adj_price+sod_top_adj_price) * (sod_qty_ord-sod_qty_shp),4) * so_ex_rate,0),nvl(sod_tool_cost* (sod_qty_ord-sod_qty_shp) *so_ex_rate*nvl(exd_r_rate,1),0)
      from [email protected]
      left join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from [email protected]) on so_nbr=sod_nbr and so_char1 is null
      left join [email protected] on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join [email protected] on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where between2(so_cust,var_cust1,var_cust2)=1
        and between2(so_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(so_ord_date,‘dd‘)>=var_date1
        and trunc(so_ord_date,‘dd‘)<=var_date2
        and trunc(so_ord_date,‘dd‘) >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘) or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘) or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘))
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and so_char1 is null
        and sod_status is null
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
       ---------------------------------------扣款记录-------------------------------------------------------------------
      union all
      select mrb_vend,mrb_slspsn,
             0,0,0,0,0,0,0,0,
             (case when mrb_payment=‘CP0A‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--销售折扣加价
             (case when mrb_payment=‘CP09‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--指定运费
             (case when mrb_payment=‘CP07‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--开模费
             (case when mrb_payment=‘CP08‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--认证费
             (case when mrb_payment=‘CP12‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end), --已扣rom备件预留金
             (case when mrb_payment=‘CP13‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end), --已扣市场费
             (case when (mrb_payment=‘CP01‘ and mrb_pst_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)) then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end), --已扣差异额度
             0, --已扣工具费
             0,0,0,0,0,0,0,0
      --from sdh_hist join mrb_mstr on mrb_nbr=sdh_dn_dn
      from mrb_mstr
      join mrbd_det on mrb_nbr = mrbd_nbr
      left join exd_det on exd_curr=mrbd_curr and mrb_date between exd_eff_date and exd_end_date
      where between2(mrb_vend,var_cust1,var_cust2)=1
        and between2(mrb_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(mrb_date,‘dd‘)>=var_date1 and trunc(mrb_date,‘dd‘)<=var_date2
        and mrb_prog_code=‘SLBKMTA1‘
        and mrb_pst = 1
        --and mrb_payment in (‘CP0A‘,‘CP09‘,‘CP07‘,‘CP08‘,‘CP12‘,‘CP13‘,‘CP01‘)
        --and sdh_crt_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)  -----金额管控从这个时间点开始
        /*注:扣款记录必须从12月8号下午2点整(程序更新时间) 以后审核过账的数据才能算是有效扣款数据,并在报表中过滤无效扣款数据*/
      union all
      select mrb_vend,mrb_slspsn,
             0,0,0,0,0,0,0,0,
             (case when mrb_payment=‘CP0A‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--销售折扣加价
             (case when mrb_payment=‘CP09‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--指定运费
             (case when mrb_payment=‘CP07‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--开模费
             (case when mrb_payment=‘CP08‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end),--认证费
             (case when mrb_payment=‘CP12‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end), --已扣rom备件预留金
             (case when mrb_payment=‘CP13‘ then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end), --已扣市场费
             (case when (mrb_payment=‘CP01‘ and mrb_pst_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)) then mrbd_price *mrbd_qty * nvl(exd_rate,1) else 0 end), --已扣差异额度
             0, --已扣工具费
             0,0,0,0,0,0,0,0
      --from [email protected] join [email protected] on mrb_nbr=sdh_dn_dn
      from [email protected]
      join [email protected] on mrb_nbr = mrbd_nbr
      left join [email protected] on exd_curr=mrbd_curr and mrb_date between exd_eff_date and exd_end_date
      where between2(mrb_vend,var_cust1,var_cust2)=1
        and between2(mrb_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(mrb_date,‘dd‘)>=var_date1 and trunc(mrb_date,‘dd‘)<=var_date2
        and mrb_prog_code=‘SLBKMTA1‘
        and mrb_pst = 1
        --and mrb_payment in (‘CP0A‘,‘CP09‘,‘CP07‘,‘CP08‘,‘CP12‘,‘CP13‘,‘CP01‘)
        --and sdh_crt_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)   -----金额管控从这个时间点开始
        ---客户退款单
      union all
      select crf_vend,crf_slspsn,
             0,0,0,0,0,0,0,0,
             0,
             0,
             0,
             0,
             0,
             0,
             crfd_price *crfd_qty * nvl(exd_rate,1)*(-1), --DMZ返款
             0,
             0,0,0,0,0,0,0,0
      --from sdh_hist join mrb_mstr on mrb_nbr=sdh_dn_dn
      from crf_mstr
      join crfd_det on crf_nbr = crfd_nbr
      left join exd_det on exd_curr=crfd_curr and crf_date between exd_eff_date and exd_end_date
      where between2(crf_vend,var_cust1,var_cust2)=1
        and between2(crf_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(crf_date,‘dd‘)>=var_date1 and trunc(crf_date,‘dd‘)<=var_date2
        and crf_prog_code=‘SLBKMTA2‘
        and crf_pst = 1
        and crf_type=‘005‘
      union all
      select crf_vend,crf_slspsn,
             0,0,0,0,0,0,0,0,
             0,
             0,
             0,
             0,
             0,
             0,
             crfd_price *crfd_qty * nvl(exd_rate,1)*(-1), --DMZ返款
             0,
             0,0,0,0,0,0,0,0
      --from sdh_hist join mrb_mstr on mrb_nbr=sdh_dn_dn
      from [email protected]
      join [email protected] on crf_nbr = crfd_nbr
      left join [email protected] on exd_curr=crfd_curr and crf_date between exd_eff_date and exd_end_date
      where between2(crf_vend,var_cust1,var_cust2)=1
        and between2(crf_slspsn,var_slspsn1,var_slspsn2)=1
        and trunc(crf_date,‘dd‘)>=var_date1 and trunc(crf_date,‘dd‘)<=var_date2
        and crf_prog_code=‘SLBKMTA2‘
        and crf_pst = 1
        and crf_type=‘005‘
      -----------------------------------------代客采购申购单工具费扣款----------------------------------------------------------------------
      union all
      select pr_cust,pr_req_by,
             0,0,0,0,0,0,0,0,
             0,
             0,
             0,
             0,
             0,
             0,
             0,
             req_price*req_qty* nvl(exd_rate,1), --已扣工具费
             0,0,0,0,0,0,0,0
      from pr_mstr
      join req_mstr on pr_nbr = req_pr_nbr
      left join exd_det on exd_curr=pr_curr and pr_rel_date between exd_eff_date and exd_end_date
      where 1=1
      and between2(pr_cust,var_cust1,var_cust2)=1
      and between2(pr_req_by,var_slspsn1,var_slspsn2)=1
      and trunc(pr_rel_date,‘dd‘)>=var_date1 and trunc(pr_rel_date,‘dd‘)<=var_date2
      and pr_prog_code=‘PUPRMTA7‘
      and pr_pst = 1
      and pr_payment=‘CP05B‘
      union all
      select pr_cust,pr_req_by,
             0,0,0,0,0,0,0,0,
             0,
             0,
             0,
             0,
             0,
             0,
             0,
             req_price*req_qty* nvl(exd_rate,1), --已扣工具费
             0,0,0,0,0,0,0,0
      from [email protected]
      join [email protected] on pr_nbr = req_pr_nbr
      join [email protected] on cm_addr = pr_cust
      left join [email protected] on exd_curr=pr_curr and pr_rel_date between exd_eff_date and exd_end_date
      where 1=1
      and between2(pr_cust,var_cust1,var_cust2)=1
      and between2(pr_req_by,var_slspsn1,var_slspsn2)=1
      and trunc(pr_rel_date,‘dd‘)>=var_date1 and trunc(pr_rel_date,‘dd‘)<=var_date2
      and pr_prog_code=‘PUPRMTA7‘
      and pr_pst = 1
      and pr_payment=‘CP05B‘
      ------------------------------------------代客采购申购单工具费扣款 end-----------------------------------------------------------------
      union all
      select b.certp_cust,b.certp_sp_slspsn,
             0,0,0,0,0,0,0,0,
             (case when b.certp_payment=‘CP0A‘ then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--销售折扣加价
             (case when b.certp_payment=‘CP09‘ then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--指定运费
             (case when b.certp_payment=‘CP07‘ then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--开模费
             (case when b.certp_payment=‘CP08‘ then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--认证费
             (case when b.certp_payment=‘CP12‘ then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--已扣rom备件预留金
             (case when b.certp_payment=‘CP13‘ then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--已扣市场费
             (case when (b.certp_payment=‘CP01‘ and b.certp_pst_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)) then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--已扣差异额度
             0,
             0,0,0,0,0,0,0,0
      from certpd_det a join certp_mstr b on b.certp_nbr=a.certpd_nbr
      where between2(b.certp_cust,var_cust1,var_cust2)=1
        and between2(b.certp_sp_slspsn,var_slspsn1,var_slspsn2)=1
        and b.certp_date>=var_date1 and b.certp_date<=var_date2
        and b.certp_prog_code=‘ADPAMTA1‘
        and b.certp_pst=1
        --and b.certp_pst_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)   -----金额管控从这个时间点开始
      union all
      select b.certp_cust,b.certp_sp_slspsn,
             0,0,0,0,0,0,0,0,
             (case when b.certp_payment=‘CP0A‘ then a.certpd_cert_cost* [email protected](b.certp_curr, b.certp_date) else 0 end),--销售折扣加价
             (case when b.certp_payment=‘CP09‘ then a.certpd_cert_cost* [email protected](b.certp_curr, b.certp_date) else 0 end),--指定运费
             (case when b.certp_payment=‘CP07‘ then a.certpd_cert_cost* [email protected](b.certp_curr, b.certp_date) else 0 end),--开模费
             (case when b.certp_payment=‘CP08‘ then a.certpd_cert_cost* [email protected](b.certp_curr, b.certp_date) else 0 end),--认证费
             (case when b.certp_payment=‘CP12‘ then a.certpd_cert_cost* [email protected](b.certp_curr, b.certp_date) else 0 end),--已扣rom备件预留金
             (case when b.certp_payment=‘CP13‘ then a.certpd_cert_cost* [email protected](b.certp_curr, b.certp_date) else 0 end),--已扣市场费
             (case when (b.certp_payment=‘CP01‘ and b.certp_pst_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)) then a.certpd_cert_cost*GetExRate(b.certp_curr, b.certp_date) else 0 end),--已扣差异额度
             0,
             0,0,0,0,0,0,0,0
      from [email protected] a join [email protected] b on b.certp_nbr=a.certpd_nbr
      where between2(b.certp_cust,var_cust1,var_cust2)=1
        and between2(b.certp_sp_slspsn,var_slspsn1,var_slspsn2)=1
        and b.certp_date>=var_date1 and b.certp_date<=var_date2
        and b.certp_prog_code=‘ADPAMTA1‘
        and b.certp_pst=1
        --and b.certp_pst_date >= to_date(‘2016-12-08 14:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)   -----金额管控从这个时间点开始
      union all
      ---------------------------------------ROM/DMZ扣款单------------------------------------------------------------------------------
      select adb_cust,adb_slspsn,
             0,0,0,0,0,0,0,0,
             (case when adb_payment in (‘010‘) then adb_amt *GetExRate(adb_curr , adb_date ) else 0 end),--销售折扣加价
             (case when adb_payment in (‘007‘) then adb_amt *GetExRate(adb_curr , adb_date ) else 0 end),--指定运费
             (case when adb_payment in (‘008‘) then adb_amt *GetExRate(adb_curr , adb_date ) else 0 end),--开模费
             (case when adb_payment in (‘009‘) then adb_amt *GetExRate(adb_curr , adb_date ) else 0 end),--认证费
             (case when adb_payment in (‘001‘,‘002‘,‘003‘) then adb_amt *GetExRate(adb_curr , adb_date ) else 0 end),--已扣rom备件预留金
             (case when adb_payment in (‘004‘,‘005‘) then adb_amt *GetExRate(adb_curr , adb_date ) else 0 end), --已扣市场费均摊
             (case when adb_payment=‘006‘ then adb_amt *GetExRate(adb_curr , adb_date ) else 0 end),--已扣差异额度
             0,
             0,0,0,0,0,0,0,0
      from adb_mstr
      where 1=1
      and between2(adb_cust,var_cust1,var_cust2)=1
      and between2(adb_slspsn,var_slspsn1,var_slspsn2)=1
      and adb_pst = 1
      and adb_date>=var_date1 and adb_date<=var_date2
      -----------------------------------------ROM/DMZ预留费用分配-------------------------------------------------------------------------------------
      union all
      select adc_cust,adc_slspsn,
             adcd_list_amt,adcd_freight_amt,adcd_mould_amt,adcd_attest_amt,0,adcd_market_amt,0,adcd_tool_amt,
             0,--销售折扣加价
             0,--指定运费
             0,--开模费
             0,--认证费
             (case when adcd_type = ‘001‘ then (adcd_list_amt+adcd_freight_amt+adcd_mould_amt+adcd_attest_amt+adcd_market_amt+adcd_sales_amt+adcd_tool_amt) else 0 end),--已扣rom备件预留金
             0, --已扣市场费均摊
             (case when adcd_type = ‘002‘ then (adcd_list_amt+adcd_freight_amt+adcd_mould_amt+adcd_attest_amt+adcd_market_amt+adcd_sales_amt+adcd_tool_amt) else 0 end),--已扣差异额度
             0,
             0,0,0,0,0,0,0,0
      from adc_mstr
      left join adcd_det on adcd_nbr = adc_nbr
      where 1=1
      and between2(adc_cust ,var_cust1,var_cust2)=1
      and between2(adc_slspsn ,var_slspsn1,var_slspsn2)=1
      and adc_pst = 1
      and adc_date >= var_date1 and adc_date <= var_date2
      ----------------------------------------------运费扣款-------------------------------------------------------------------------
      union all
      select frb_cust,frb_slspsn,
             0,0,0,0,0,0,0,0,
             0,--销售折扣加价
             (case when (frbd_costype = ‘CP09‘ OR frbd_costype = ‘001‘) then frbd_feight_cost *GetExRate(frbd_curr, frb_date) else 0 end),--指定运费
             0,--开模费
             0,--认证费
             0,--已扣rom备件预留金
             0, --已扣市场费均摊
             0,--已扣差异额度
             0,
             0,0,0,0,0,0,0,0
      from frb_mstr
      left join frbd_det on frbd_nbr = frb_nbr
      where 1=1
      and between2(frb_cust ,var_cust1,var_cust2)=1
      and between2(frb_slspsn ,var_slspsn1,var_slspsn2)=1
      and frb_pst  = 1
      and frb_date >=var_date1 and frb_date <=var_date2
     ----------------------------------------------期初金额数------------------------------------------------------------------
     union all
     select gend_name,cm_slspsn,
            to_number(nvl(gend_property5,0)),to_number(nvl(gend_property4,0)),to_number(nvl(gend_property2,0)),to_number(nvl(gend_property3,0)),
            to_number(nvl(gend_property6,0)),to_number(nvl(gend_property7,0)),to_number(nvl(gend_property8,0)),to_number(nvl(gend_property9,0)),
            0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0
     from gend_det join cm_mstr on cm_addr=gend_name
     where 1=1
       and between2(gend_name,var_cust1,var_cust2)=1
       and between2(cm_slspsn,var_slspsn1,var_slspsn2)=1
       --and to_date(gend_property1,‘yyyymmdd‘) between var_date1 and var_date2
       and gend_gen=‘SOYU_BEGIN‘ and gend_disabled=0
    ) group by sdh_cust,sdh_so_slspsn;

   declare cursor cur_cust is
   select tpt_cust,tpt_slspsn from tpt_slbkso1;
   begin
   open cur_cust;
   loop
     fetch cur_cust into var_cust , var_slspsn;
     exit when cur_cust%notfound;

 --下单单价差异总金额
    select count(1),sum(mould_total),sum(attest_total),sum(freight_total),sum(market_total),sum(list_total),sum(rom_total),sum(diff_total),sum(tool_amt)
    into var_count,var_mould_amt,var_attest_amt,var_freight_amt,var_market_amt,var_list_amt,var_rom_amt,var_diff_amt,var_tool_amt
    from (
    select nvl(sod_mould_cost* sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as mould_total,
       nvl(sod_attest_cost* sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as attest_total,
       nvl(sod_freight_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as freight_total,
       nvl(sod_market_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as market_total,
       nvl(sod_list_cost*sod_qty_ord*so_ex_rate*nvl(exd_r_rate,1),0) as list_total,
       nvl(sod_rom_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as rom_total,
       --nvl((sod_adj_price+sod_sp_adj_price+sod_top_adj_price) * SOD_QTY_ORD * so_ex_rate,0) as diff_total
       nvl(get_so_sub_price2(sod_nbr,sod_line) * so_ex_rate,0) as diff_total,
       nvl(sod_tool_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) tool_amt
    from sod_det
    join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from so_mstr) on so_nbr=sod_nbr  and so_char1 is null
    left join gend_det on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
    left join exd_det on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
    where 1=1
        and so_cust = var_cust
        and so_slspsn = var_slspsn
        and so_ord_date>=var_date1
        and so_ord_date<=var_date2
        and so_ord_date >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and sod_status is null
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
    union all
    select nvl(sod_mould_cost* sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as mould_total,
       nvl(sod_attest_cost* sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as attest_total,
       nvl(sod_freight_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as freight_total,
       nvl(sod_market_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as market_total,
       nvl(sod_list_cost*sod_qty_ord*so_ex_rate*nvl(exd_r_rate,1),0) as list_total,
       nvl(sod_rom_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) as rom_total,
       --nvl((sod_adj_price+sod_sp_adj_price+sod_top_adj_price) * SOD_QTY_ORD * so_ex_rate,0) as diff_total
       nvl([email protected](sod_nbr,sod_line)  *so_ex_rate,0) as diff_total,
       nvl(sod_tool_cost*sod_qty_ord *so_ex_rate*nvl(exd_r_rate,1),0) tool_amt
    from [email protected]
      join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from [email protected]) on so_nbr=sod_nbr and so_char1 is null
      left join [email protected] on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join [email protected] on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where 1=1
        and so_cust = var_cust
        and so_slspsn = var_slspsn
        and so_ord_date>=var_date1 and so_ord_date<=var_date2
        and so_ord_date >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and sod_status is null
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
     union all
     select nvl(sod_mould_cost* sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as mould_total,
       nvl(sod_attest_cost* sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as attest_total,
       nvl(sod_freight_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as freight_total,
       nvl(sod_market_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as market_total,
       nvl(sod_list_cost*sod_qty_shp*so_ex_rate*nvl(exd_r_rate,1),0) as list_total,
       nvl(sod_rom_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as rom_total,
       --nvl((sod_adj_price+sod_sp_adj_price+sod_top_adj_price) * SOD_QTY_ORD * so_ex_rate,0) as diff_total
       nvl(get_so_sub_price2(sod_nbr,sod_line) * so_ex_rate,0) as diff_total,
       nvl(sod_tool_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) tool_amt
    from sod_det
    join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from so_mstr) on so_nbr=sod_nbr  and so_char1 is null
    left join gend_det on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
    left join exd_det on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
    where 1=1
        and so_cust = var_cust
        and so_slspsn = var_slspsn
        and so_ord_date>=var_date1
        and so_ord_date<=var_date2
        and so_ord_date >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        --and SOD_CLOSE_BY = ‘SYSTEM‘
        and sod_status = ‘C‘
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
    union all
    select nvl(sod_mould_cost* sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as mould_total,
       nvl(sod_attest_cost* sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as attest_total,
       nvl(sod_freight_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as freight_total,
       nvl(sod_market_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as market_total,
       nvl(sod_list_cost*sod_qty_shp*so_ex_rate*nvl(exd_r_rate,1),0) as list_total,
       nvl(sod_rom_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) as rom_total,
       --nvl((sod_adj_price+sod_sp_adj_price+sod_top_adj_price) * SOD_QTY_ORD * so_ex_rate,0) as diff_total
       nvl([email protected](sod_nbr,sod_line)  *so_ex_rate,0) as diff_total,
       nvl(sod_tool_cost*sod_qty_shp *so_ex_rate*nvl(exd_r_rate,1),0) tool_amt
    from [email protected]
      join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from [email protected]) on so_nbr=sod_nbr and so_char1 is null
      left join [email protected] on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
      left join [email protected] on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
      where 1=1
        and so_slspsn = var_slspsn
        and so_cust = var_cust
        and so_ord_date>=var_date1
        and so_ord_date<=var_date2
        and so_ord_date >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
        and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
        and so_pst=1 and so_prog_code=‘SLSOMTA1‘
        --and SOD_CLOSE_BY = ‘SYSTEM‘
        and sod_status = ‘C‘
        and (sod_mould_cost+sod_attest_cost+sod_freight_cost+sod_market_cost+sod_list_cost+sod_rom_cost+sod_adj_price+sod_sp_adj_price) <> 0
     union all
     select
       nvl(sod_mould_cost* CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as mould_total,
       nvl(sod_attest_cost* CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as attest_total,
       nvl(sod_freight_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as freight_total,
       nvl(sod_market_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as market_total,
       nvl(sod_list_cost*CRD_QTY_RTN*(-1)*so_ex_rate*nvl(exd_r_rate,1),0) as list_total,
       nvl(sod_rom_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as rom_total,
       --nvl((sod_adj_price+sod_sp_adj_price+sod_top_adj_price)* CRD_QTY_RTN*(-1)*so_ex_rate,0) as diff_total
       nvl(get_so_nbr_price1(crd_cr,crd_line)* CRD_QTY_RTN*(-1)*so_ex_rate,0) as diff_total,
       nvl(sod_tool_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0)
     from crd_det
     join sod_det on crd_so = sod_nbr and crd_so_line = sod_line
     left join cr_mstr on cr_cr=crd_cr
     left join so_mstr on so_nbr = sod_nbr
     left join pt_mstr on pt_part=sod_part
     left join gend_det on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
     left join exd_det on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
     where 1=1
     and cr_pst=1
     and so_char1 is null
     and so_ord_date >= var_date1
     and so_ord_date <= var_date2
     and so_ord_date >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
     and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
     and so_slspsn = var_slspsn
     and so_cust = var_cust
    union all
    select
       nvl(sod_mould_cost* CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as mould_total,
       nvl(sod_attest_cost* CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as attest_total,
       nvl(sod_freight_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as freight_total,
       nvl(sod_market_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as market_total,
       nvl(sod_list_cost*CRD_QTY_RTN*(-1)*so_ex_rate*nvl(exd_r_rate,1),0) as list_total,
       nvl(sod_rom_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0) as rom_total,
       --nvl((sod_adj_price+sod_sp_adj_price+sod_top_adj_price)* CRD_QTY_RTN*(-1)*so_ex_rate,0) as diff_total
       nvl([email protected](crd_cr,crd_line)* CRD_QTY_RTN*(-1)*so_ex_rate,0) as diff_total,
       nvl(sod_tool_cost*CRD_QTY_RTN*(-1) *so_ex_rate*nvl(exd_r_rate,1),0)
    from [email protected]
    join [email protected] on crd_so = sod_nbr and crd_so_line = sod_line
    left join [email protected] on cr_cr=crd_cr
    left join (select so_nbr,so_cust,so_slspsn,so_ex_rate,so_ord_date,so_pst,so_prog_code,so_char1,SO_QUOTE_PROJECT from [email protected]) on so_nbr = sod_nbr
    left join [email protected] on pt_part=sod_part
    left join [email protected] on Gend_Option=SO_QUOTE_PROJECT and GEND_GEN=‘QUOTE_PROJECT‘
    left join [email protected] on exd_curr=GEND_PROPERTY7 and so_ord_date between exd_eff_date and exd_end_date
   where 1=1
    and cr_pst=1
    and so_char1 is null
    and so_ord_date >= var_date1
    and so_ord_date <= var_date2
    and so_ord_date >= to_date(‘2016-06-01‘,‘yyyy-mm-dd‘)
    and ((so_nbr like ‘SOPA%‘ and so_nbr >= ‘SOPA1606‘)
       or (so_nbr like ‘SO%‘ and so_nbr not like ‘SOPA%‘ and so_nbr >= ‘SO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FGSO%‘ and so_nbr >= ‘FGSO1606‘)
       or (so_nbr like ‘GSO%‘ and so_nbr >= ‘GSO1606‘)
       or (so_nbr like ‘FSO%‘ and so_nbr >= ‘FSO1606‘)
       or (so_nbr like ‘PSO%‘ and so_nbr >= ‘PSO1606‘)
       or (so_nbr like ‘QSO%‘ and so_nbr >= ‘QSO1606‘)
       or (so_nbr like ‘ESO%‘ and so_nbr >= ‘ESO1606‘)
     )
    and so_slspsn = var_slspsn
    and so_cust = var_cust
        )
        ;

    select sum(adcd_list_amt),sum(adcd_freight_amt),sum(adcd_mould_amt),sum(adcd_attest_amt),sum(adcd_market_amt),sum(adcd_tool_amt)
      into var_list_amt1,var_freight_amt1,var_mould_amt1,var_attest_amt1,var_market_amt1,var_tool_amt1
      from adc_mstr
      left join adcd_det on adcd_nbr = adc_nbr
      where 1=1
      and adc_pst = 1
      and adc_cust = var_cust
      and adc_slspsn = var_slspsn
      and adc_date >= var_date1 and adc_date <= var_date2;

    update tpt_slbkso1 set
         TPT_MOULD_TOTAL = nvl(var_mould_amt,0)+nvl(var_mould_amt1,0),
         TPT_FREIGHT_TOTAL = nvl(var_freight_amt,0)+nvl(var_freight_amt1,0),
         TPT_ATTEST_TOTAL = nvl(var_attest_amt,0)+nvl(var_attest_amt1,0),
         TPT_LIST_TOTAL = nvl(var_list_amt,0)+nvl(var_list_amt1,0),
         TPT_ROM_TOTAL = nvl(var_rom_amt,0),
         TPT_MARKET_TOTAL = nvl(var_market_amt,0)+nvl(var_market_amt1,0),
         TPT_SOD_DIFF_TOTAL = nvl(var_diff_amt,0),
         TPT_TOOL_TOTAL = nvl(var_tool_amt,0)+nvl(var_tool_amt1,0)
    where 1=1
    and tpt_cust = var_cust and tpt_slspsn = var_slspsn;
    end loop;
    close cur_cust;
    end;

    /*  期初金额
      均摊费用期初金额(描述=客户代码;属性1=年月日;属性2=开模费均摊;属性3=认证费均摊;属性4=指定运费;
                                  属性5=销售折扣加价;属性6=ROM备件预留金;属性7=市场费均摊;属性8=差异总额)

     select to_number(nvl(gend_property2,0)),to_number(nvl(gend_property3,0)),to_number(nvl(gend_property4,0)),to_number(nvl(gend_property5,0)),
           to_number(nvl(gend_property6,0)),to_number(nvl(gend_property7,0)),to_number(nvl(gend_property8,0))
     into var_mould_amt2,var_attest_amt2,var_freight_amt2,var_list_amt2,var_rom_amt2,var_market_amt2,var_diff_amt2
     from gend_det join cm_mstr on cm_addr=gend_name
     where gend_name = var_cust
       and between2(cm_slspsn,var_slspsn1,var_slspsn2)=1
       and to_date(gend_property1,‘yyyymmdd‘) between var_date1 and var_date2
       and gend_gen=‘SOYU_BEGIN‘ and gend_disabled=0;*/
     merge into tpt_slbkso1
     using(
     select gend_name,to_number(nvl(gend_property2,0)) as mould_amt,to_number(nvl(gend_property3,0)) as attest_amt,
           to_number(nvl(gend_property4,0)) as freight_amt,to_number(nvl(gend_property5,0)) as list_amt,
           to_number(nvl(gend_property6,0)) as rom_amt,to_number(nvl(gend_property7,0)) as market_amt,to_number(nvl(gend_property8,0)) as diff_amt,
           to_number(nvl(gend_property9,0)) as tool_amt
     from gend_det join cm_mstr on cm_addr=gend_name
     where 1=1
       and between2(gend_name,var_cust1,var_cust2)=1
       and between2(cm_slspsn,var_slspsn1,var_slspsn2)=1
       --and to_date(gend_property1,‘yyyymmdd‘) between var_date1 and var_date2
       and gend_gen=‘SOYU_BEGIN‘ and gend_disabled=0
     ) t
     on (t.gend_name = tpt_cust)
     when matched then
       update set
         TPT_MOULD_TOTAL = TPT_MOULD_TOTAL + t.mould_amt,
         TPT_FREIGHT_TOTAL = TPT_FREIGHT_TOTAL + t.freight_amt,
         TPT_ATTEST_TOTAL = TPT_ATTEST_TOTAL + t.attest_amt,
         TPT_LIST_TOTAL = TPT_LIST_TOTAL + t.list_amt,
         TPT_ROM_TOTAL = TPT_ROM_TOTAL + t.rom_amt,
         TPT_MARKET_TOTAL = TPT_MARKET_TOTAL + t.market_amt,
         TPT_SOD_DIFF_TOTAL = TPT_SOD_DIFF_TOTAL + t.diff_amt,
         TPT_TOOL_TOTAL =  TPT_TOOL_TOTAL + t.tool_amt;

   /*open resultcursor for
     select t.* ,cm_sort,usr_name,‘RMB‘ as tpt_curr,
            t.tpt_sod_list_amt-t.tpt_mrbd_list_amt as sub_list_amt,
            t.tpt_sod_freight_amt-t.tpt_mrbd_freight_amt as sub_freihgt_amt,
            t.tpt_sod_mould_amt-t.tpt_mrbd_mould_amt as sub_mould_amt,
            t.tpt_sod_attest_amt-t.tpt_mrbd_attest_amt as sub_attest_amt ,
            t.tpt_sod_rom_amt-t.tpt_mrbd_rom_amt as sub_rom_amt,
            t.tpt_sod_market_amt-t.tpt_mrbd_market_amt as sub_market_amt,
            t.tpt_sod_diff_amt - TPT_MRBD_DIFF_AMT as sub_diff_amt,

            TPT_LIST_TOTAL as sod_list_amt1,
            TPT_FREIGHT_TOTAL as sod_freight_amt1,
            TPT_MOULD_TOTAL as sod_mould_amt1,
            TPT_ATTEST_TOTAL as sod_attest_amt1,
            TPT_ROM_TOTAL as sod_rom_amt1,
            TPT_MARKET_TOTAL as sod_market_amt1,
            TPT_SOD_DIFF_TOTAL as sod_diff_amt1,

            TPT_LIST_TOTAL -t.tpt_mrbd_list_amt as sub_list_amt1,
            TPT_FREIGHT_TOTAL-t.tpt_mrbd_freight_amt as sub_freihgt_amt1,
            TPT_MOULD_TOTAL-t.tpt_mrbd_mould_amt as sub_mould_amt1,
            TPT_ATTEST_TOTAL-t.tpt_mrbd_attest_amt as sub_attest_amt1,
            TPT_ROM_TOTAL-t.tpt_mrbd_rom_amt as sub_rom_amt1,
            TPT_MARKET_TOTAL-t.tpt_mrbd_market_amt as sub_market_amt1,
            TPT_SOD_DIFF_TOTAL-t.tpt_mrbd_diff_amt as sub_diff_amt1
     from tpt_slbkso1 t
     left join cm_mstr on cm_addr=t.tpt_cust
     left join usr_mstr on usr_user=t.tpt_slspsn
where 1=1*/
   return;
exception
  when others then rollback;
end slbksoiqa2;

  

时间: 2024-11-09 03:58:01

Oracle 销售统计报表 实例的相关文章

销售统计饼状图

销售统计,画出饼状图(动态) 技术: Hightchart (前端) 思路:1.关联查询几张表获取数据  2.dao层将sql语句转成hql语句 3.service层   4Action 5.前端  注意配置文件 1.关联查询几张表获取数据 //操作的对象:(开始日期(创建订单的日期) orders),商品类型goodstype,销售额(sum()orderdetail); 找出关联的中间表是goods, 条件查询的是日期是orderdetail //1.数据库实现查询 --1.关联4张表 se

Oracle 分区表的统计信息实例

ORACLE的统计信息在执行SQL的过程中扮演着非常重要的作用,而且ORACLE在表的各个层次都会有不同的统计信息,通过这些统计信息来描述表的,列的各种各样的统计信息.下面通过一个复合分区表来说明一些常见的和不常见的统计信息. 搭建测试用例 --创建表. SQL> create table test partition by range(object_id) subpartition by hash(object_type) subpartitions 4 (partition p1 value

oracle高级查询(实例基于scott用户四张表)

oracle高级查询(实例基于scott用户四张表) 分组查询 多表查询 子查询 综合实例 ======================================================================= scott用户的四张表(emp,dept,bonus,salgrade) 没有这四张表的可参考http://blog.csdn.net/love_legain/article/details/54311040进行创建 -----------------------

Oracle自动性能统计

Oracle自动性能统计 ??高效诊断性能问题,需要提供完整可用的统计信息,好比医生给病人看病的望闻问切,才能够正确的确诊,然后再开出相应的药方.Oracle数据库为系统.会话以及单独的sql语句生成多种类型的累积统计信息.本文主要描述oracle性能统计涉及到的相关概念及统计对象,以更好的利用统计信息为性能调整奠定基础. 一.性能统计信息的几个术语 增量值(delta) ??性能统计信息依赖于delta值的累计,即增量值.增量值反映了在某个特定时段某个特定事件的趋势,或者说波动的幅度.通过对比

MongoDB报表实例方案选型

MongoDB报表实例方案选型 背景介绍 在我们的生产环境使用的是复制集,为了将数据库服务器的业务压力分摊,我们将数据库拆分到了不同的复制集上运行. 我们在MongoDB复制集上运行应用程序,有时候有报表需求,常规用途是获得用户行为的分析,还有其他商业定制指标数据:有搜索引擎的查询需求,使用Solr从oplog.rs获取增量数据更新产品信息的索引. 这些报表查询和搜索引擎的查询需求,尽量不能影响到线上的业务正常运行,因此不能直接在生产数据库上运行报表.经过开发和运维讨论之后,在项目成立之初,计划

Ultimus流程化费控系统——直观统计报表,财务管理的好帮手

流程化费控系统——直观统计报表,财务管理的好帮手 财务开支管理直接影响着企业的整体运营,Ultimus流程化费控系统基于平台特色的“流程+报表”模式,结合流程进展中的预算与报销.借款与还款等实时数据明细,按照各角色用户的意愿定制生成报表.同时,为便捷领导层管理和支撑决策,Ultimus流程化费控系统支持数据表格和统计图结合显示,根据查询结果,自动显示统计图,统计图展示直观的预算执行概貌,表格则提供关联的明晰化数据. 1.Ultimus流程化费控系统——费控报表多样式展现系统为使用者提供饼状.柱状

非常规统计报表之制作按段分组报表的新办法

报表开发中,经常会碰到一些需要进行非常规统计的报表,固定分组.可重复分组.组内排序,还包括跨行组计算的报表,甚至有些报表本身无数据来源.以及需要对数据源再计算.这些报表本身具备一定的特殊性,使用常规方法往往难于实现. 对于按段分组报表,各段之间可以有重复,也就可能出现按段可重复的分组报表.集算报表在完成这类特殊统计报表时比较简单,这里通过一个实例说明实现过程. 报表说明 根据员工基本信息表按年龄统计各年龄段区间的人数.奖金等汇总情况.报表样式如下: 这里"30-40岁"和"3

Oracle排名函数(Rank)实例详解

这篇文章主要介绍了Oracle排名函数(Rank)实例详解,需要的朋友可以参考下 --已知:两种排名方式(分区和不分区):使用和不使用partition --两种计算方式(连续,不连续),对应函数:dense_rank,rank ·查询原始数据:学号,姓名,科目名,成绩 select * from t_score S_ID S_NAME SUB_NAME SCORE 1 张三 语文 80.00 2 李四 数学 80.00 1 张三 数学 0.00 2 李四 语文 50.00 3 张三丰 语文 1

Oracle之索引(Index)实例讲解 - 基础

Oracle之索引(Index)实例讲解 - 基础 索引(Index)是关系数据库中用于存放表中每一条记录位置的一种对象,主要目的是加快数据的读取速度和数据的完整性检查.索引的建立是一项技术性要求非常高的工作. 一般在数据库设计阶段就要考虑到如何设计和创建索引. 1. 创建索引 创建索引的语法: CREATE [UNIQUE] INDEX [schema.] index ON [schema.] table (column [ASC | DESC], column [ASC | DESC]...