有一张表TEST,3个列日期,     号码,当日资金额
2011-01-01  123   100
2011-01-02  123   50
2011-01-03  123   200
2011-01-04  123   150想实现下面的结果,(累计就是从最早1-1号的数据累加得到)
能取出一段时间内的数据,日期>='开始日期' and 日期<='结束日期'日期,     号码,当日资金额,累计资金额
2011-01-03  123     200         350
2011-01-04  123     150         500

解决方案 »

  1.   

    SELECT datetime
        , seq
        , SUM(SUM(bankroll)) over (ORDER BY seq ROWS UNBOUNDED PRECEDING)FROM test
    GROUP BY datetime, seq
      

  2.   


    SQL> with t as(
      2       select '2011-01-01' dt,123 nm,100 curr_money from dual union all
      3       select '2011-01-02',123,50 from dual union all
      4       select '2011-01-03',123,200 from dual union all
      5       select '2011-01-04',123,150 from dual)
      6  select * from (
      7  select dt,nm,curr_money,
      8         sum(curr_money) over (order by dt rows unbounded preceding) cum_money
      9  from t)
     10  where dt>='2011-01-03' and dt<='2011-01-04'
     11  /
     
    DT                 NM CURR_MONEY  CUM_MONEY
    ---------- ---------- ---------- ----------
    2011-01-03        123        200        350
    2011-01-04        123        150        500
      

  3.   

    日期的比较应该是将字符串转换为日期类型,前提是dt也是date类型:
    where dt >= to_date('2011-01-03','yyyy-mm-dd') 
      and dt <= to_date('2011-01-04','yyyy-mm-dd')