有两个表,表A和表B
表A结构:BillID(单据ID),UnitID(单位ID),TheDate(日期),SumMoney(合计金额)  主键BillID
表B结构:ID(主键),BillID(单据ID),Money(金额)       ID是自动ID关系:表A的BillID对表B的BillID是一对多例如:
表A:BillID   UnitID   TheDate   SumMoney
   1       1      2007-1-1   10000
   2       1      2007-1-2   20000 
   3       2      2001-1-3   52000表B
ID    BillID    Money
  1     1         100
  2     1         500
  3     2         1000
  4     2         1000
  5     2         100需要生成一个数据视图为:
BillID   UnitID   TheDate  SumMoney  Money    NotMoney
单据号     单位      日期      合计金额    已付金额  未付金额
  1        1       2007-1-1  10000     600      9400
  2        1       2007-1-2  20000     2200     8800
  3        2       2007-1-3  52000     0        52000同时还可以对日期和单位再次查询求这个语句.在此先谢过了 

解决方案 »

  1.   

    第二条应是2100元,即主表的BillID等于子表BillID的合计需要生成一个数据视图为: 
    BillID   UnitID   TheDate  SumMoney  Money    NotMoney 
    单据号     单位      日期      合计金额    已付金额  未付金额 
      1        1       2007-1-1  10000     600      9400 
      2        1       2007-1-2  20000     2100     8800 
      3        2       2007-1-3  52000     0        52000 
      

  2.   

    BillID   UnitID   TheDate  SumMoney  Money    NotMoney  
    单据号     单位      日期      合计金额    已付金额  未付金额  
    1        1       2007-1-1  10000     600      9400  
    2        1       2007-1-2  20000     2100     8800  
    3        2       2007-1-3  52000     0        52000   20000     2100     8800 
    钱数算错了吧
      

  3.   

    填错了BillID   UnitID   TheDate  SumMoney  Money    NotMoney   
    单据号     单位      日期      合计金额    已付金额  未付金额   
    1        1       2007-1-1  10000     600      9400   
    2        1       2007-1-2  20000     2100     18900   
    3        2       2007-1-3  52000     0        52000   
      

  4.   

    今天怎么搞的.回回错了BillID   UnitID   TheDate  SumMoney  Money    NotMoney    
    单据号     单位      日期      合计金额    已付金额  未付金额(合计金额-已付金额) 
    1        1       2007-1-1  10000     600      9400    
    2        1       2007-1-2  20000     2100     17900    
    3        2       2007-1-3  52000     0        52000   
      

  5.   

    select A.BillID,A.UnitID,A.TheDate,A.SumMoney,B.[Money],A.SumMoney-B.[Money] as NotMoney 
    from A 
    left join B 
    on A.BillID = B.BillID试试 
    备注:我没建环境测试,难免错误;
      

  6.   

    同时还可以对日期和单位再次查询 
    ============================
    select A.BillID,A.UnitID,A.TheDate,A.SumMoney,B.[Money],A.SumMoney-B.[Money] as NotMoney  
    from A  
    left join B  
    on A.BillID = B.BillID 
    where A.TheDate = '你的查询条件' and A.UnitID = 你的查询条件
      

  7.   

    试试select A.*,t.payed Money,A.SumMoney-t.payed as NotMoney from A, (select billid, count(money) payed from B group by billid) t where t.billid = a.billid(+)
      

  8.   

    测试通过:select A.BillID, A.UnitID, A.TheDate, A.SumMoney, isnull(B.Money,0), A.SumMoney - isnull(B.Money,0) as NotMoney
    from Table_A A 
    left join (select BillID, sum(Money) as Money from Table_B group by BillID ) B
    on A.BillID = B.BillID
      

  9.   

    select A.BillID,A.UnitID,A.TheDate,A.SumMoney,ISNULL(C.[Money],0),ISNULL(A.SumMoney-C.[Money],A.SumMoney) as NotMoney from A 
    left join 
    (select BillID,sum([money]) as [Money] from B group by BillID) C
    on A.BillID =c.BillID晚了一步
    ~~~~~create table A(BillID int,UnitID int,TheDate datetime,SumMoney int)
    insert into A select 1,1,'2007-1-1',10000
        union all select 2,1,'2007-1-2',20000 
        union all select 3,2,'2001-1-3',52000 insert into B select 1,1,100 
        union all select 2,1,500 
        union all select 3,2,1000 
        union all select 4,2,1000 
        union all select 5,2,100 --执行结果
    BillID      UnitID      TheDate                                                SumMoney                NotMoney    
    ----------- ----------- ------------------------------------------------------ ----------- ----------- ----------- 
    1           1           2007-01-01 00:00:00.000                                10000       600         9400
    2           1           2007-01-02 00:00:00.000                                20000       2100        17900
    3           2           2001-01-03 00:00:00.000                                52000       0           52000(所影响的行数为 3 行)
      

  10.   

    select a.billid,a.unitid,a.thedate,a.summoney,b.money,(a.summoney-b.money)as leftmoney from a left join b on a.billid = b.billid已经测试过的
    好用 
      

  11.   

    结贴,ldarmy和sqllong
    请顶一下贴给分
      

  12.   

    结贴,ldarmy和sqllong 
    请顶一下贴给分
    http://topic.csdn.net/u/20070929/12/78306796-abf1-4961-92ad-461818a84ef4.html?seed=147269000