TABLE中加入了
2000/3/5 60 10
是变成这
日期 收入 支出
2000/3/1 50 30
2000/3/2 45 60
2000/3/5 60 10
2000/3/5 60 10
还是这
2000/3/1 50 30
2000/3/2 45 60
2000/3/5 120 20

解决方案 »

  1.   

    我觉的加一行后还是这样好
    日期 收入 支出
    2000/3/1 50 30
    2000/3/2 45 60
    2000/3/5 60 10
    2000/3/5 60 10
    然后
    create view b as
    select acc_day,sum(debit),sum(credit)
      from a
     group by acc_day;
      

  2.   

    然后:
    select acc_day,debit,credit,debit-credit+surplus  from (
    select acc_day,debit,credit,
           lag(debit-credit,1,0) over(order by acc_day) surplus 
     from  b )
      

  3.   

    这不叫做改。我顺便修正一下:
    create view b(acc_day,debitas,credit)
    select acc_day,sum(debit),sum(credit)
      from a
     group by acc_day;就这样了,不会错的。
      

  4.   

    樓上的,請問lag,over函數是干什麼的,lag(debit-credit,1,0) over(order by acc_day)是什麼意思?你的結果有點問題 ,是
    2000/3/1 50 30 20
    2000/3/2 45 60 5
    2000/3/5 60 10 35
      

  5.   

    把你机器上lag(debit-credit,1,0) over(order by acc_day)后的结果列出来
      

  6.   

    1. SELECT 日期,收入,支出,收入-支出 余额 FROM tablename
      

  7.   

    select thedate,income,outgo,(income-outgo+surplus0)remain from (
    select thedate,income,outgo,
           lag(surplus+income-outgo,1,0) over(order by thedate) surplus0
     from(
     select thedate,income,outgo,
           lag(income-outgo,1,0) over(order by thedate) surplus
    from  inout)
    )
    這是我寫出來的,但是數據多余三行就有問題了,哪位大人指點一下應該怎麼改
      

  8.   

    SQL> select * from inout;THEDATE       INCOME      OUTGO
    --------- ---------- ----------
    01-MAR-00         50         30
    02-MAR-00         45         60
    05-MAR-00         60         10
    05-MAR-00         60         10SQL> select every_thedate,sum_income,sum_outgo,add_surplus
      2          from (select every_thedate,nvl(sum(income),0) sum_income,nvl(sum(outgo),0) sum_outgo,
      3          sum(nvl(sum(income),0)-nvl(sum(outgo),0)) over(order by every_thedate) add_surplus
      4                  from (select nvl((select min(thedate) from inout ),sysdate) + rownum -1 every_t
    hedate
      5                         from emp where rownum <= nvl((select max(thedate) - min(thedate) 
      6               from inout ),sysdate) + 1 ) a,
      7        inout b
      8  where a.every_thedate = b.thedate(+)
      9  group by every_thedate) 
     10  /EVERY_THE SUM_INCOME  SUM_OUTGO ADD_SURPLUS
    --------- ---------- ---------- -----------
    01-MAR-00         50         30          20
    02-MAR-00         45         60           5
    03-MAR-00          0          0           5
    04-MAR-00          0          0           5
    05-MAR-00        120         20         105
      

  9.   

    onejune4450,感謝你的不吝賜教,你開始給我的那個程序有點小問題,我改了一下,可是還是隻能對三行數據有效,如果再多一行,就要再加入一層select。迷惑中!
      

  10.   

    我第二問也得出結果了:
    select thedate,income,outgo,(income-outgo+surplus1)remain from (
    select thedate,income,outgo,
           lag(surplus0+income-outgo,1,0) over(order by thedate) surplus1
     from(
    select thedate,income,outgo,
           lag(surplus+income-outgo,1,0) over(order by thedate) surplus0
     from(
     select thedate,income,outgo,
           lag(suminout,1,0) over(order by thedate) surplus
    from  (select thedate,sum(income) income,sum(outgo) outgo,(sum(income)-sum(outgo))suminout from inout group by thedate))
    ))
    THEDATE        INCOME      OUTGO     REMAIN
    ---------- ---------- ---------- ----------
    01-3月 -00         50         30         20
    02-3月 -00         45         60          5
    05-3月 -00        120         20        105
      

  11.   

    你的第二问解决,可能到第四行就不对了,第二问的解决是:
    select thedate,sum(income),sum(outgo),
           sum(income-outgo) over(partition thedate order by thedate) surplus
      from inout b
     where group by thedate
      

  12.   

    第一问的解决:
    select thedate,income,outgo,
           sum(income-outgo) over(order by thedate) surplus
      from inout b
      

  13.   

    over(order by thedate) 這句話我還是沒懂。書上沒看到over的用法
      

  14.   

    这一句有点毛病
     rownum <= nvl((select max(thedate) - min(thedate) from inout ),sysdate) + 1,
     --------------------------------------
    改为:
     rownum <= nvl((select max(thedate) - min(thedate) from inout ),0) + 1,
    如果不要最后一天可改为:
     rownum <= nvl((select max(thedate) - min(thedate) from inout ),0) 
    变为
      

  15.   

    第二問還是有問題,那個看上去好象是多余的where去了也不行
      

  16.   

    我也是新上来看别人用才开始试着用,我有几年没碰oracle了。
      

  17.   

    第二問:
    select thedate,sum_income,sum_outgo,
           sum(sum_income-sum_outgo) over(order by thedate) surplus
      from ( select thedate,sum(income) sum_income,sum(outgo) sum_outgo from inout group by thedate)
      

  18.   

    为安全期间这一句的0,要改为-1
    rownum <= nvl((select max(thedate) - min(thedate) from inout ),-1) + 1,