本帖最后由 arthljoo 于 2009-07-06 22:54:12 编辑

解决方案 »

  1.   

    declare @a table(单位 nvarchar(10),欠款 money)
    insert into @a select 'a',10
          union all select 'b',6
          union all select 'c',12
    declare @b table(单位 nvarchar(10),已交金额 money)
    insert into @b select 'a',10
          union all select 'b',2
          union all select 'e',5  
     select a.单位,欠款=a.欠款-isnull(b.已交金额,0) from @a a left join @b b on a.单位=b.单位单位         欠款
    ---------- ---------------------
    a          0.00
    b          4.00
    c          12.00(3 行受影响)
      

  2.   


    --> 测试时间:2009-07-06 22:57:39
    --> 我的淘宝: http://shop36766744.taobao.com/if object_id('[表1]') is not null drop table [表1]
    create table [表1]([单位] varchar(1),[欠款] int)
    insert [表1]
    select 'a',10 union all
    select 'b',6 union all
    select 'c',12if object_id('[表2]') is not null drop table [表2]
    create table [表2]([单位] varchar(1),[已交金额] int)
    insert [表2]
    select 'a',10 union all
    select 'b',2 union all
    select 'e',5select  单位=isnull(a.单位,b.单位),
    欠款=abs(isnull(已交金额,0)-isnull(欠款,0))
    from 表1 a full join 表2 b on a.单位=b.单位 where abs(isnull(已交金额,0)-isnull(欠款,0))<>0
    /*
    单位   欠款          
    ---- ----------- 
    b    4
    e    5
    c    12(所影响的行数为 3 行)
    */drop table 表1,表2
      

  3.   

    select 
      isnull(a.单位,b.单位) as 单位,
      isnull(a.欠款,0)-isnull(b.已交金额,0) as 欠款 
    from
      (select 单位,sum(欠款) as 欠款 from 表1 group by 单位) a
    full join
      (select 单位,sum(已交金额) as 已交金额 from 表2 group by 单位) b
    on
      a.单位=b.单位
    where
      isnull(a.欠款,0)-isnull(b.已交金额,0)>0
      

  4.   

    这么晚还有高人在啊 呵呵 。谢谢大家 。 不好意思, 标题里忘了写  是基于 access 的。
      

  5.   


    declare @a table(单位 nvarchar(10),欠款 money)
    insert into @a select 'a',10
          union all select 'b',6
          union all select 'c',12
    declare @b table(单位 nvarchar(10),已交金额 money)
    insert into @b select 'a',10
          union all select 'b',2
          union all select 'e',5  select isnull(a.单位,'e') 单位,ABS(isnull(a.欠款,0)-isnull(b.已交金额,0)) as 欠款 from @a a full outer join @b b on a.单位=b.单位
    a .0000
    b 4.0000
    e 5.0000
    c 12.0000
      

  6.   

    说明:
    1、当已交金额大余欠款时,应当显示负数,财务角度
    2、还款可以是多次declare @a table(单位 nvarchar(10),欠款 money)
    insert into @a select 'a',10
          union all select 'b',6
          union all select 'c',12
    declare @b table(单位 nvarchar(10),已交金额 money)
    insert into @b select 'a',5
    union all select 'a',6
          union all select 'b',2  
          union all select 'b',2
          union all select 'c',2
          union all select 'e',5  
    --select * from @a
    --select * from @b
     select isnull(a.单位,b.单位) 单位,
    ABS(isnull(a.欠款,0)-isnull(b.已交金额,0)) as 欠款 from @a a full outer join @b b on a.单位=b.单位
    select a.单位,(isnull(a.欠款,0)-isnull((select sum(b.已交金额) from @b b where a.单位=b.单位),0)) as  欠款 from @a  a
    where  (isnull(a.欠款,0)-isnull((select sum(b.已交金额) from @b b where a.单位=b.单位),0))<>0
    union 
    select c.单位,(-1)*sum(c.已交金额) from @b c where not exists (select 1 from @a d where c.单位=d.单位) group by c. 单位
      

  7.   

    此处似乎不该用left join 毕竟有可能有没有欠款的记录 却有还款的记录的可能
    应该是用full join