比如查2003-01-15的余额:
select name,sum(dayin)-sum(dayout)
from income
where dt<='2003-01-15'
group by name
order by name

解决方案 »

  1.   

    declare @income table (dt datetime,name varchar(10),dayin int,dayout int)
    insert @income values('2002-1-1','aa',5,3)
    insert @income values('2002-1-1','bb',3,4)
    insert @income values('2002-1-2','bb',7,5)
    insert @income values('2002-1-3','aa',5,3)
    select  c.dt,c.name,isnull(a.dayin,0) dayin,isnull(a.dayout,0) dayout,(select  sum(isnull(dayin,0)-isnull(dayout,0))  from  @income where  name=c.name  and  dt<=c.dt)  net_income
    from  @income  a right join (select dt,name from (select distinct dt from @income )aaa ,(select distinct name from @income) bbb) c on a.dt=c.dt and a.name=c.name order  by  c.dt,c.name
      

  2.   

    icevi(按钮工厂) ,这两种都写出来,我比较一下
      

  3.   

    insert @income values('2002-1-1','aa',5,3)
    insert @income values('2002-1-1','bb',3,4)
    insert @income values('2002-1-3','aa',5,3)
    select top 1000 identity(int,1,1) id into # from sysobjects a,sysobjects b,sysobjects c
    select  c.dt,c.name,isnull(a.dayin,0) dayin,isnull(a.dayout,0) dayout,(select  sum(isnull(dayin,0)-isnull(dayout,0))  from  @income where  name=c.name  and  dt<=c.dt)  net_income
    from  @income  a right join (select dt,name from (select dateadd(day,id,dmin-1) dt from (select min(dt) dmin,datediff(day,min(dt),max(dt))+1 f from @income) a join # b on a.f>=b.id)aaa ,(select distinct name from @income) bbb) c on a.dt=c.dt and a.name=c.name order  by  c.dt,c.name drop table #
      

  4.   

    declare @income table (dt datetime,name varchar(10),dayin int,dayout int)
    insert @income values('2002-1-1','aa',5,3)
    insert @income values('2002-1-1','bb',3,4)
    insert @income values('2002-1-3','aa',5,3)
    select top 1000 identity(int,1,1) id into # from sysobjects a,sysobjects b,sysobjects c
    select  c.dt,c.name,isnull(a.dayin,0) dayin,isnull(a.dayout,0) dayout,(select  sum(isnull(dayin,0)-isnull(dayout,0))  from  @income where  name=c.name  and  dt<=c.dt)  net_income
    from  @income  a right join (select dt,name from (select dateadd(day,id,dmin-1) dt from (select min(dt) dmin,datediff(day,min(dt),max(dt))+1 f from @income) a join # b on a.f>=b.id)aaa ,(select distinct name from @income) bbb) c on a.dt=c.dt and a.name=c.name order  by  c.dt,c.name drop table #
      

  5.   

    pengdali(大力) ,谢谢。但是我要的是不用临时表,不是全部日期的也行,(只有发生过的日期)你20:37:00 的方法可行吗?
      

  6.   

    这两个字段
    日合计收入  dayin  
    日合计支出  dayout
    的默认值是多少?
    默认值是‘null’吧?
      

  7.   

    我不是写了  isnull(列,0)
      

  8.   

    pengdali(大力) ,谢谢。我正在测试,似乎没什么问题。自连接的妙用我还得体会体会。
      

  9.   

    declare @income table (dt datetime,name varchar(10),dayin int,dayout int)
    insert @income values('2002-1-1','aa',5,3)
    insert @income values('2002-1-1','bb',3,4)
    insert @income values('2002-1-3','aa',5,3)select  c.dt,c.name,isnull(a.dayin,0) dayin,isnull(a.dayout,0) dayout,(select  sum(isnull(dayin,0)-isnull(dayout,0))  from  @income where  name=c.name  and  dt<=c.dt)  net_income
    from  @income  a right join (select dt,name from (select distinct dt from @income )aaa ,(select distinct name from @income) bbb) c on a.dt=c.dt and a.name=c.name order  by  c.dt,c.name---如果这样就少了一天的!那你只能用第二种方法!