select YMDHM,DTRN,D=(
select sum(DTRN) from a 
where YMDHM<=aa.YMDHM and datediff(day,YMDHM,a.YMDHM)=0)
from a aa

解决方案 »

  1.   

    --改一下:select YMDHM,DTRN,D=(
    select sum(DTRN) from a 
    where YMDHM<=aa.YMDHM and datediff(day,YMDHM,aa.YMDHM)=0)
    from a aa
      

  2.   

    --测试--测试数据
    create table a(YMDHM Datetime,DTRN Numeric(5,1))
    insert a  select '2004-4-8 23:00:00',  1
    union all select '2004-4-9 00:00:00',  0
    union all select '2004-4-9 01:00:00',0.5
    union all select '2004-4-9 02:00:00',  1
    union all select '2004-4-9 03:00:00',0.5
    union all select '2004-4-9 04:00:00',1.5
    go--查询
    select YMDHM,DTRN,D=(
    select sum(DTRN) from a 
    where YMDHM<=aa.YMDHM and datediff(day,YMDHM,aa.YMDHM)=0)
    from a aa
    go--删除测试
    drop table a/*--测试结果YMDHM                                                  DTRN    D       
    ------------------------------------------------------ ------- --------
    2004-04-08 23:00:00.000                                1.0     1.0
    2004-04-09 00:00:00.000                                .0      .0
    2004-04-09 01:00:00.000                                .5      .5
    2004-04-09 02:00:00.000                                1.0     1.5
    2004-04-09 03:00:00.000                                .5      2.0
    2004-04-09 04:00:00.000                                1.5     3.5(所影响的行数为 6 行)--*/
      

  3.   

    zjcxc(: 邹建 :) 向你学习