表A:ID     Date     Client     NoPay     YesPay
1     2012-02-01     张三     1000     0
2     2012-02-02     张三     0        500
3     2012-02-03     张三     800      0
4     2012-02-02     李四     600      0
5     2012-02-02     李四     0        500
6     2012-02-03     李四     0        100B表:ID     Date     Client     NoPay     YesPay     Surplus
1     2012-02-01     张三     1000     0       1000
2     2012-02-02     张三     0        500     500
3     2012-02-03     张三     800      0       1300
4     2012-02-02     李四     600      0       600
5     2012-02-02     李四     0        500     100
6     2012-02-03     李四     0        100     0由A得到B的结果

解决方案 »

  1.   

    --ID     Date     Client     NoPay     YesPay
    --1     2012-02-01     张三     1000     0
    --2     2012-02-02     张三     0        500
    --3     2012-02-03     张三     800      0
    --4     2012-02-02     李四     600      0
    --5     2012-02-02     李四     0        500
    --6     2012-02-03     李四     0        100
    if OBJECT_ID('tb')is not null
    drop table tb
    go
     create table tb (ID int,Date datetime,Client varchar(50), NoPay int,YesPay int)
    insert into tb values(1 ,    '2012-02-01'   ,  '张三'   ,  1000    , 0)
    insert into tb values(2 ,    '2012-02-02' ,    '张三'   ,  0      ,  500)
    insert into tb values(3  ,   '2012-02-03'  ,   '张三'   ,  800   ,   0)
    insert into tb values(4  ,   '2012-02-02'  ,   '李四'  ,   600   ,   0)
    insert into tb values(5  ,   '2012-02-02'  ,   '李四'   ,  0     ,   500)
    insert into tb values(6  ,   '2012-02-03' ,    '李四'  ,   0    ,    100)
    --ID     Date     Client     NoPay     YesPay     Surplus
    --1     2012-02-01     张三     1000     0       1000
    --2     2012-02-02     张三     0        500     500
    --3     2012-02-03     张三     800      0       1300
    --4     2012-02-02     李四     600      0       600
    --5     2012-02-02     李四     0        500     100
    --6     2012-02-03     李四     0        100     0
    ;with ct as (
    select *,(select SUM(NoPay-YesPay) from tb where  ID=t.ID )s from tb t )
    select c.ID,c.Date,c.Client,c.NoPay,c.YesPay,
    (select SUM(s) from ct where Client=c.Client and Date<=c.Date and ID<=c.ID )Surplus from ct c 
    ID          Date                    Client                                             NoPay       YesPay      Surplus
    ----------- ----------------------- -------------------------------------------------- ----------- ----------- -----------
    1           2012-02-01 00:00:00.000 张三                                                 1000        0           1000
    2           2012-02-02 00:00:00.000 张三                                                 0           500         500
    3           2012-02-03 00:00:00.000 张三                                                 800         0           1300
    4           2012-02-02 00:00:00.000 李四                                                 600         0           600
    5           2012-02-02 00:00:00.000 李四                                                 0           500         100
    6           2012-02-03 00:00:00.000 李四                                                 0           100         0(6 行受影响)
      

  2.   

    with ct as ()
    这是什么用法,思路我大致看明白了,只是这句不通,现在是into ct然后得到我想要的结果
      

  3.   

    select id,date,client,nopay,yespay,
    (select sum(s) from (select *,(nopay-yespay)s from tb) t where t.id<=a.id and t.client=a.client) 
    from (select *,(nopay-yespay)s from tb) a
      

  4.   

    呵呵。你是什么数据库。with公用表表达式(CTE)简化嵌套
      

  5.   

    --ID     Date     Client     NoPay     YesPay
    --1     2012-02-01     张三     1000     0
    --2     2012-02-02     张三     0        500
    --3     2012-02-03     张三     800      0
    --4     2012-02-02     李四     600      0
    --5     2012-02-02     李四     0        500
    --6     2012-02-03     李四     0        100
    if OBJECT_ID('tb')is not null
    drop table tb
    go
     create table tb (ID int,Date datetime,Client varchar(50), NoPay int,YesPay int)
    insert into tb values(1 ,    '2012-02-01'   ,  '张三'   ,  1000    , 0)
    insert into tb values(2 ,    '2012-02-02' ,    '张三'   ,  0      ,  500)
    insert into tb values(3  ,   '2012-02-03'  ,   '张三'   ,  800   ,   0)
    insert into tb values(4  ,   '2012-02-02'  ,   '李四'  ,   600   ,   0)
    insert into tb values(5  ,   '2012-02-02'  ,   '李四'   ,  0     ,   500)
    insert into tb values(6  ,   '2012-02-03' ,    '李四'  ,   0    ,    100)
    --ID     Date     Client     NoPay     YesPay     Surplus
    --1     2012-02-01     张三     1000     0       1000
    --2     2012-02-02     张三     0        500     500
    --3     2012-02-03     张三     800      0       1300
    --4     2012-02-02     李四     600      0       600
    --5     2012-02-02     李四     0        500     100
    --6     2012-02-03     李四     0        100     0
     select c.ID,c.Date,c.Client,c.NoPay,c.YesPay,
    (select SUM(s) from (select *,(select SUM(NoPay-YesPay) from tb where  ID=t.ID )s from tb t 
    )a where Client=c.Client  and ID<=c.ID )Surplus 
    from (select *,(select SUM(NoPay-YesPay) from tb where  ID=t.ID )s from tb t 
    ) c 
     
    ID          Date                    Client                                             NoPay       YesPay      Surplus
    ----------- ----------------------- -------------------------------------------------- ----------- ----------- -----------
    1           2012-02-01 00:00:00.000 张三                                                 1000        0           1000
    2           2012-02-02 00:00:00.000 张三                                                 0           500         500
    3           2012-02-03 00:00:00.000 张三                                                 800         0           1300
    4           2012-02-02 00:00:00.000 李四                                                 600         0           600
    5           2012-02-02 00:00:00.000 李四                                                 0           500         100
    6           2012-02-03 00:00:00.000 李四                                                 0           100         0(6 行受影响)
      

  6.   

    还有一种情况就是:
    A表:
    ID     Date     Client     NoPay     YesPay
    7     2012-02-05     王五     0        200
    8     2012-02-06     王五     0        160
    9     2012-02-07     王五     100      0
    B表:
    ID     Date     Client     NoPay     YesPay     Surplus
    7     2012-02-05     王五     0        200     0
    8     2012-02-06     王五     0        160     0
    9     2012-02-07     王五     100      0       100按照上面的方法 Surplus会是-260 而不是100
    我的方法是使用case语句,小于0就让它等于0,大家有没更好的解决方法