有个库存表,结构如下
月份     名称       数量
1       cpu        5
1       主板        3
2       cpu       10
2       主板       4
3       cpu       2
3       主板       5
上表是每个月按月输入,输入后想要在数据库中保存成
下面的样子
月份     名称       数量
1       cpu       5
1       主板       3
2       cpu       15
2       主板       7
3       cpu       17
3       主板       12
有什么方法可以实现?

解决方案 »

  1.   


    --测试数据库为oracle--测试表
    create table test (c_month int,c_name varchar2(100),c_num int);
    --测试数据
    insert into test
    select 1,'cpu',5 from dual union all 
    select 1,'主板',3 from dual union all 
    select 2,'cpu',10 from dual union all
    select 2,'主板',4 from dual union all
    select 3,'cpu',2 from dual union all 
    select 3,'主板',5  from dual;
    --获得累计数函数
    create or replace function getNum(cmonth int,cname varchar2) return int
    is
      num int;
    begin
         select nvl(sum(c_num),0) into num from test where c_month<=cmonth and c_name=cname;
         return num;
    end;
    --执行查询
    select c_month,c_name,getnum(c_month,c_name)
    from test 
    group by c_month,c_name
    --查询结果
    1 cpu 5
    1 主板 3
    2 cpu 15
    2 主板 7
    3 cpu 17
    3 主板 12
      

  2.   

    select 月份,名称,sum(数量)
    from 表
    group by 月份,名称
      

  3.   

    --access
    SELECT t1.c_month,t1.c_name,(select sum(t.c_num) from test t where t.c_month<=t1.c_month and 
    t.c_name=t1.c_name) as sum 
    from test t1 group by t1.c_month,t1.c_name;
      

  4.   

    你为什么要这样设计呢,库存表就应该是保存最新的库存数据,是一个实时变化的数据表。
    在你的库存表显示的结果应该算是一个入库记录表吧,入库时增加库存,你还应该有一个出库的记录表,出库时减库存。
    按你想要的结果,应该是保存小于等于当前月的入库汇总信息,你可以每个月存一次。比如3月
    insert into 表2
    select 3,名称,sum(数量)
    from 表1
    group by 名称
      

  5.   


    create table test (c_month int,c_name varchar(100),c_num int)insert into test
    select 1,'cpu',5  union all 
    select 1,'主板',3  union all 
    select 2,'cpu',10  union all
    select 2,'主板',4  union all
    select 3,'cpu',2  union all 
    select 3,'主板',5 select 
    c_month,
    c_name,
    isnull((select sum(c_num) from test where c_name=a.c_name and c_month<a.c_month),0)+c_num
    from test a/*
    c_month     c_name                                                                                                           
    ----------- ---------------------------------------------------------------------------------------------------- ----------- 
    1           cpu                                                                                                  5
    1           主板                                                                                                   3
    2           cpu                                                                                                  15
    2           主板                                                                                                   7
    3           cpu                                                                                                  17
    3           主板                                                                                                   12(所影响的行数为 6 行)
    */
      

  6.   

    没想到月亮老大是搞oracle的呀
      

  7.   

    zxf_feng 阿日
    在你的库存表显示的结果应该算是一个入库记录表吧,入库时增加库存
    按你想要的结果,应该是保存小于等于当前月的入库汇总信息,你可以每个月存一次。
    To:zxf_feng 阿日
    就是你说的这个意思,每个月实现累加,一月就是一月的库存,二月是一月加二月的库存,三月是一月加二月加三月
      

  8.   

    查询我写好了,没有什么问题
    但是往库存表中insert的时候总是提示当前提供程序不支持从单一执行返回多个记录集
      

  9.   

    你用的是adoquery吧
    adoquery.Open;替换成adoquery.ExecSQL
      

  10.   

    To:失踪的月亮,我咋没注意执行Insert要用ExecSQL了这里,十分感谢!还是自己太粗心了