select  
a.etr_date, --日期
a.good_id,  --物品id
a.good_nm,  --名
(select (nvl(sum(b.loc_qty1),0) - nvl(sum(b.loc_qty2),0) - nvl(sum(b.loc_qty3),0) + nvl(sum(b.loc_qty4),0)) allsum  from TB_SCM_STOCK b where a.good_id = b.good_id 
and (to_date(b.etr_date, 'yyyy-mm-dd hh24:mi:ss') < to_date(a.etr_date, 'yyyy-mm-dd hh24:mi:ss'))) allsum, --汇总loc_qty1 按小于该条记录日期汇总
a.loc_qty1,a.loc_qty2,a.loc_qty3,a.loc_qty4,  --这些是数量加减信息 
(select (nvl(sum(b.loc_qty1),0) - nvl(sum(b.loc_qty2),0) - nvl(sum(b.loc_qty3),0) + nvl(sum(b.loc_qty4),0)) allsum  from TB_SCM_STOCK b where a.good_id = b.good_id 
and (to_date(b.etr_date, 'yyyy-mm-dd hh24:mi:ss') <= to_date(a.etr_date, 'yyyy-mm-dd hh24:mi:ss'))) outsum, --汇总loc_qty1 按小于等于该条记录日期汇总,经过加减后的汇总
a.dlr_id   --用户ID
from TB_SCM_STOCK a这个sql运行起来性能不好哪位大侠帮我优化优化
同一个表按没条记录 都显示着条记录日期之前的总和用good_id 分组oraclesql优化性能

解决方案 »

  1.   

     select distinct dept_id
         ,sum(case when 1=1 and tt.create_time >'{0}' and tt.create_time<'{1}' then 1 else 0 end) over(partition by tt.dept_id) all_total
         ,sum(case when 1=1 and tt.finish_endtime >'{0}' and tt.finish_endtime<'{1}'   then 1 else 0 end) over(partition by tt.dept_id) need_finish_total
         ,sum(case when 1=1 and tt.task_status='9' and tt.finish_endtime > '{0}' and tt.finish_endtime <'{1}'    then 1 else 0 end) over(partition by tt.dept_id) finish_total
         ,sum(case when 1=1 and tt.task_status='8' and tt.finish_endtime >'{0}' and tt.finish_endtime<'{1}'   then 1 else 0 end) over(partition by tt.dept_id) cancel_total
          from (select distinct tt.*,
                   from t_taskinfo tt                   
                   where 1 = 1 and tt.task_source ='1' 
                   and tt.task_type='1'          
                  ) tt  
    这个分析函数  希望你能看懂~~
      

  2.   

    模拟了一个简单的表 希望对你理解分析函数有所帮助 select distinct id      
    ,sum( case when 1=1 then A else 0 end) over(partition by id) --按Id汇总A字段的和
    ,sum( case when 1=1 then A+B+C else 0 end) over(partition by id)--按Id汇总所有
    ,sum( case when 1=1 and A=2  then A else 0 end) 
    over(partition by id)--按Id汇总A字段的和(条件是A=2)
    from
        --以下是模拟表          
       (  select 1 id, 2 A, 2 B, 3 C
           from dual
         union
         select 2 id, 2 A, 3 B, 4 C
           from dual
         union
         select 2 id, 4 A, 4 B, 5 C from dual
         )
       
      

  3.   

    select  
    a.etr_date,
    a.good_id,
    a.good_nm,a.loc_qty1,a.loc_qty2,a.loc_qty3,a.loc_qty4,
    sum(a.loc_qty1 - a.loc_qty2 - a.loc_qty3 + a.loc_qty4) over (partition by a.good_id order by a.etr_date) allsum,
    a.dlr_id
    from TB_SCM_STOCK a我现在用这个SQL也能实现一部分,问题是这个实现的是allsum可以实现加减所得,但是这之前想要个初始值
    比如 第一行的-15 作为第2行的初始值
    fistsum  loc_qty1   loc_qty2   loc_qty3  loc_qty4 allsum 
    -15         0        14           0        0       -29
    算法就是-15 + 0 -14 - 0 +0 -29
    就是说每次初始值都是从头sum到当前行的上一行的值,或者说每行的最后一个列是下一行的第一列
      

  4.   

    select etr_date,good_id,good_nm,lag(allsum, 1) over(partition by good_id order by etr_date) firstsum,
    loc_qty1,loc_qty2,loc_qty3,loc_qty4,allsum
     from (
    select  
    a.etr_date,
    a.good_id,
    a.good_nm,
    a.loc_qty1,a.loc_qty2,a.loc_qty3,a.loc_qty4,
    sum(a.loc_qty1 - a.loc_qty2 - a.loc_qty3 + a.loc_qty4) over (partition by a.good_id order by a.etr_date) allsum,
    a.dlr_id
    from TB_SCM_STOCK a
    用这个解决了,性能不错