表1  cid  bid 都是自增
            cid name
             1   A公司
             2   b公司
             3   c公司
            ...    ....   
    表2            bid cid name
             1   1   小王
             2   2   小明
             3   3   小张
             4   2   小王
         
            ... ...   ....
   表三
 
 时间            bid    应付金额    以付金额     cid  
2008-08-08       1      1000          800        1
2008-09-08       1       0            200        1
2008-10-08       3      1200          600        3
2008-10-08       3      1000           0         3
2008-10-08       3       0            600        3
2008-11-08       4      800           600        2
    
==========================================================得到下面这个表
   name    A公司     B公司     C公司    ...    未付金额
   小王    0.00      200       0.00      ...      200
   小张    0.00      200       600      ...      800
  

解决方案 »

  1.   

    --> 测试数据: [表1]
    if object_id('[表1]') is not null drop table [表1]
    create table [表1] (cid int,name varchar(5))
    insert into [表1]
    select 1,'A公司' union all
    select 2,'b公司' union all
    select 3,'c公司'
    --> 测试数据: [表2]
    if object_id('[表2]') is not null drop table [表2]
    create table [表2] (bid int,cid int,name varchar(4))
    insert into [表2]
    select 1,1,'小王' union all
    select 2,2,'小明' union all
    select 3,3,'小张' union all
    select 4,2,'小王'
    --> 测试数据: [表三]
    if object_id('[表三]') is not null drop table [表三]
    create table [表三] (时间 datetime,bid int,应付金额 int,以付金额 int,cid int)
    insert into [表三]
    select '2008-08-08',1,1000,800,1 union all
    select '2008-09-08',1,0,200,1 union all
    select '2008-10-08',3,1200,600,3 union all
    select '2008-10-08',3,1000,0,3 union all
    select '2008-10-08',3,0,600,3 union all
    select '2008-11-08',4,800,600,2
    declare @sql varchar(8000)
    set @sql='select a.name'
    select @sql=@sql+',['+name+']=sum(case a.cid when '''+ltrim(cid)+''' then c.应付金额-c.以付金额 else 0 end)'
    from 表1
    set @sql=@sql+',未付金额=sum(c.应付金额-c.以付金额) from 表2 a,表三 c where a.bid=c.bid and a.cid=c.cid group by a.name'
    exec(@sql)
      

  2.   

    CREATE TABLE TA([cid] INT, [name] NVARCHAR(3))
    INSERT TA 
    SELECT 1, N'A公司' UNION ALL 
    SELECT 2, N'b公司' UNION ALL 
    SELECT 3, N'c公司'CREATE TABLE TB([bid] INT, [cid] INT, [name] NVARCHAR(2))
    INSERT TB 
    SELECT 1, 1, N'小王' UNION ALL 
    SELECT 2, 2, N'小明' UNION ALL 
    SELECT 3, 3, N'小张' UNION ALL 
    SELECT 4, 2, N'小王'CREATE TABLE TC([时间] DATETIME, [bid] INT, [应付金额] INT, [以付金额] INT, [cid] INT)
    INSERT TC 
    SELECT '2008-08-08', 1, 1000, 800, 1 UNION ALL 
    SELECT '2008-09-08', 1, 0, 200, 1 UNION ALL 
    SELECT '2008-10-08', 3, 1200, 600, 3 UNION ALL 
    SELECT '2008-10-08', 3, 1000, 0, 3 UNION ALL 
    SELECT '2008-10-08', 3, 0, 600, 3 UNION ALL 
    SELECT '2008-11-08', 4, 800, 600, 2
    DECLARE @STR NVARCHAR(4000)
    SET @STR=N''
    SELECT @STR=@STR+N','+N'SUM(CASE WHEN A.[name]=N'''+[name]+N''' THEN [应付金额]-[以付金额] ELSE 0 END) AS '+[name]
    FROM TA
    GROUP BY [name]EXEC(N'SELECT B.name'+@STR+N',总金额 =SUM([应付金额]-[以付金额]) 
    FROM TC AS C JOIN TB AS B ON C.bid=B.bid
      JOIN TA AS A ON C.[cid]=A.[cid]
    GROUP BY B.name')DROP TABLE TA,TB,TC
    /*
    name A公司         b公司         c公司         总金额
    ---- ----------- ----------- ----------- -----------
    小张   0           0           1000        1000
    小王   0           200         0           200
    */