表如下:
ID 商品ID 日期 实在库 入库 出库
1  001    10/1         10  5
2  001    10/2      4   6
3  001    10/4          5  2
4  001    10/4             6
5  001    10/5          10

现在想得到以下信息
商品ID 日期 实在库 调整数 入库 出库
001    10/1                 10    5
001    10/2      4 (5)            6
001    10/4        (4)           11
001    10/5        (4+5-2-6)  实在库、入库、出库都是按照商品ID和日期进行Group求合计,关键是调整数的计算
调整数的计算方法是看这个商品当前日期之前是否有实在库,
如果没有 就==当前日期之前的入库总和-出库总和
如果有 就=之前的、离当前日期最近的实在库+实在库所在日期到当前日期之间的(入库总和-出库总和)初学者,麻烦各位前辈啦^^

解决方案 »

  1.   

    解释下 4 + 5 -2 -6是怎么来的
    第,2,3条数据的入库是null呀
      

  2.   

    不好意思啊,重新发一下啊
    表如下: 
    ID 商品ID 日期 实在库 入库 出库 
    1  001    10/1        10  5 
    2  001    10/2      4  6 
    3  001    10/4         5  2 
    4  001    10/4            6 
    5  001    10/5        10 
     
    现在想得到以下信息 
    商品ID 日期 实在库 调整数      入库 出库 
    001    10/1                 10    5 
    001    10/2    4  (5)        6 
    001    10/4       (4)        5    8
    001    10/5       (4+5-2-6) 10 
      

  3.   

    你可以用sql源代码模式贴出来的 那样就不会因为tab,空格,回车之类的影响格式
      

  4.   

    表如下: 
    ID 商品ID 日期 实在库 入库 出库 
    1  001    10/1        10  05 
    2  001    10/2   04   06 
    3  001    10/4        05  02 
    4  001    10/4            06 
    5  001    10/5        10 
     
    现在想得到以下信息 
    商品ID 日期 实在库 调整数      入库 出库 
    001    10/1                10   05 
    001    10/2   04 (5)       06 
    001    10/4      (4)       05   08 
    001    10/5      (4+5-2-6) 10 4+5-2-6意思就是最近的实在库是4,然后加上实在库是4的日期(10/2)到当前日期(10/5)之间的入库之和,再减去这期间的出库之和
      

  5.   

    表如下: 
    ID 商品ID 日期 实在库 入库 出库 
    1  001    10/1        10  05 
    2  001    10/2  04  06 
    3  001    10/4        05  02 
    4  001    10/4            06 
    5  001    10/5        10 
     
    现在想得到以下信息 
    商品ID 日期 实在库 调整数  入库 出库 
    001    10/1              10  05 
    001    10/2  04 (5)      06 
    001    10/4     (4)      05  08 
    001    10/5     (4+5-2-6)10 4+5-2-6意思就是最近的实在库是4,然后加上实在库是4的日期(10/2)到当前日期(10/5)之间的入库之和,再减去这期间的出库之和
      

  6.   

    with tt as(select 1 id,'001' tid,date'2009-10-1' dt,null cn,10 inn,5 oun from dual
      union all select 2,'001',date'2009-10-2',4,6,null from dual
      union all select 3,'001',date'2009-10-4',null,5,2 from dual
      union all select 4,'001',date'2009-10-4',null,null,6 from dual
      union all select 5,'001',date'2009-10-5',null,null,10 from dual)select 
      a.tid,a.dt,a.cn,nvl(max(c.cn),0)+sum(nvl(b.inn,0)-nvl(b.oun,0))chn,a.inn,a.oun
    from (select tid,dt,sum(cn)cn,sum(inn)inn,sum(oun)oun from tt group by tid,dt) a 
      left join tt b
    on a.tid=b.tid and a.dt>b.dt and b.cn is null 
      and not exists(select 1 from tt where tid=b.tid and dt>b.dt and dt<a.dt and cn is not null)
      left join tt c
    on a.dt>c.dt and a.tid=c.tid and c.cn is not null
      and not exists(select 1 from tt where tid=c.tid and dt>c.dt and dt<a.dt and cn is not null)
    group by a.tid,a.dt,a.cn,a.inn,a.oun
    order by a.dtTID DT CN CHN INN OUN
    001 2009-10-1 0 10 5
    001 2009-10-2 4 5 6
    001 2009-10-4 4 5 8
    001 2009-10-5 1 10
      

  7.   

    那还能少一个连接
    with tt as(select 1 id,'001' tid,date'2009-10-1' dt,null cn,10 inn,5 oun from dual
      union all select 2,'001',date'2009-10-2',4,6,null from dual
      union all select 3,'001',date'2009-10-4',null,5,2 from dual
      union all select 4,'001',date'2009-10-4',null,null,6 from dual
      union all select 5,'001',date'2009-10-5',null,null,10 from dual)select 
      a.tid,a.dt,a.cn,sum(nvl(b.inn,0)-nvl(b.oun,0))chn,a.inn,a.oun
    from (select tid,dt,sum(cn)cn,sum(inn)inn,sum(oun)oun from tt group by tid,dt) a 
      left join tt b
    on a.tid=b.tid and a.dt>b.dt and b.cn is null 
      and not exists(select 1 from tt where tid=b.tid and dt>b.dt and dt<a.dt and cn is not null)
    group by a.tid,a.dt,a.cn,a.inn,a.oun
    order by a.dtTID DT CN CHN INN OUN
    001 2009-10-1 0 10 5
    001 2009-10-2 4 5 6
    001 2009-10-4 0 5 8
    001 2009-10-5 -3 10
      

  8.   

    表如下: 
    ID 商品ID 日期 实在库 入库 出库 
    1  001    10/1        10  05 
    2  001    10/2  04  06 
    3  001    10/4        05  02 
    4  001    10/4            06 
    5  001    10/5        10 
     
    现在想得到以下信息 
    商品ID 日期 实在库 调整数  入库 出库 
    001    10/1              10  05 
    001    10/2  04 (5)      06 
    001    10/4     (6)      05  08 
    001    10/5     (6+5-2-6)10 应该是上面那样子
    调整数是最近的有实在库的那一天开始到当前日期之间的总入库-总出库
      

  9.   

    with tt as(select 1 id,'001' tid,date'2009-10-1' dt,null cn,10 inn,5 oun from dual
      union all select 2,'001',date'2009-10-2',4,6,null from dual
      union all select 3,'001',date'2009-10-4',null,5,2 from dual
      union all select 4,'001',date'2009-10-4',null,null,6 from dual
      union all select 5,'001',date'2009-10-5',null,null,10 from dual)select 
      a.tid,a.dt,a.cn,sum(nvl(b.inn,0)-nvl(b.oun,0))chn,a.inn,a.oun
    from (select tid,dt,sum(cn)cn,sum(inn)inn,sum(oun)oun from tt group by tid,dt) a 
      left join tt b
    on a.tid=b.tid and a.dt>b.dt --and b.cn is null 
      and not exists(select 1 from tt where tid=b.tid and dt>b.dt and dt<a.dt and cn is not null)
    group by a.tid,a.dt,a.cn,a.inn,a.oun
    order by a.dtTID DT CN CHN INN OUN
    001 2009-10-1 0 10 5
    001 2009-10-2 4 5 6
    001 2009-10-4 6 5 8
    001 2009-10-5 3 10
      

  10.   

    能否解决问题的关键是,你是否可以按条件来求和(sum),就是说,如果知道某某日期前的某某字段的和,就可以解决。按条件sum的写法:
    sum(case  when month < 6 then money else 0 end ) as moneymonth,money  为字段。 
    自己再琢磨下吧。
      

  11.   

    能不能使用子查询啊,
    比如:Select SUM(实在库) 实在库
           ,(???) 调整数
          ,SUM(入库) 入库
           ,SUM(出库) 出库
    From Table1
    Group By 商品ID,日期
    Order By 商品ID,日期
      

  12.   

    select tid,dt,sum(cn)实在库,
      (select sum(nvl(inn,0))-sum(nvl(oun,0)) from tt b
        where tid=a.tid and dt<a.dt
        and not exists(select 1 from tt where tid=b.tid and dt>b.dt and dt<a.dt and cn is not null))调整数,
      sum(inn)入库,sum(oun)出库
    from tt a
    group by tid,dt
    order by 1,2;TID DT 实在库 调整数 入库 出库
    001 2009-10-1 10 5
    001 2009-10-2 4 5 6
    001 2009-10-4 6 5 8
    001 2009-10-5 3 10