SQL SERVER 可以这样
SELECT TB1.*,YSK-(case when YISK is null then 0 else yisk end) AS syysk FROM TB1 
left JOIN (SELECT KHMC,SUM(YISK) AS  YISK FROM TB2 GROUP BY KHMC)T 
ON
TB1.KHMC=T.KHMC
access 可以使用临时表delete * from tmptable
insert into tmptalbe select * from tbA
update tmptable set 应收款=应收款-已收款 from tbB where tmptable.客户名称 =tbB.客户名称select * from tmptalbe

解决方案 »

  1.   


    大哥,在语句得该如何改呢? 现在还差个(a.应收款 - sum(b.已收款)) 得放在那里呢?
    SQL = "select 客户名称 from (select a.客户名称,(a.应收款 - b.已收款) from a left join  b on a.客户名称=b.客户名称 Where (a.应收款 - b.已收款) <> 0 Union select a.客户名称,a.应收款 From a where a.客户名称 not in(select 客户名称 from b))"
      

  2.   

    access:
    delete * from tmptable 
    insert into tmptalbe select * from tbA 
    update tmptable set 应收款=应收款-已收款 from tbB where tmptable.客户名称 =tbB.客户名称 
    select * from tmptalbe where 应收款>0SQL SERVER:
    SELECT TB1.*,YSK-(case when YISK is null then 0 else yisk end) AS syysk FROM TB1 
    left JOIN (SELECT KHMC,SUM(YISK) AS  YISK FROM TB2 GROUP BY KHMC)T 
    ON
    TB1.KHMC=T.KHMC
    HAVING SYYSK>0
    注:
    YSK=应收款
    YISK=已收款
    SYYSK=剩余应收款
      

  3.   

    上面表tmptable 结构和tbA一样
      

  4.   

    供参考:
    select 客户名称,应收款 where 应收款-(select sum 已收款 as mTotal from b where 客户名称=A.客户名称 GROUP BY 客户名称) as 总和 >0 from A roder by 客户名称
      

  5.   

    修改如下:
    在SQL测试的,
    select 客户名称,应收款 where 应收款-
    (select sum 已收款 as mTotal from b where 客户名称=A.客户名称 GROUP BY 客户名称)>0 
    from A roder by 客户名称
      

  6.   

    hwmys大哥,语句行得通!但是当A表中有数据是而B表没的时候就取不出来了!得怎么改呢.....
      

  7.   

    试试这个:
    select 客户名称,应收款 where 应收款- 
    (case when (select sum 已收款 as mTotal from b where 客户名称=A.客户名称 GROUP BY 客户名称) is null then 0 else (select sum 已收款 as mTotal from b where 客户名称=A.客户名称 GROUP BY 客户名称) end)>0 
    from A roder by 客户名称
      

  8.   


    我在access里test了
    access和oracle有差别,真麻烦
      

  9.   

    请教下"ayalicer "大哥...."is null then 0 else" 这个能用于ACCESS表中的吗!!
      

  10.   

    --where 客户名称=A.客户名称 GROUP BY 客户名称
    这里 GROUP BY 客户名称 都可以省略的