搞错了,是这样
表一  
工程    负责人1         负责人2
-------------------------------  
A            AA          AAA
b            BB          BBB
C            CC          CCC
D            DD          DDD
.........等等.....  
表二  
投标      资金      投哪个
--------------------------  
A            100     AA
A            230     AAA
C            123     CC
B            234     BB
A            234     AA
D            100     DD
.....等等,,...  
要求得到  
表一  
工程      负责人   负责人2  负责人(投标总COUNT)|总资金SUM 负责人2(COUNT)|总资金   
------------------------------------------------------------- ------------ 
A            AA     AAA           2              334         1            230
b            BB     BBB           1              234         0            0
C            CC     CCC           1              123         0            0
D            DD     DDD           1              100         0            0
.........等等.......  
 

解决方案 »

  1.   

    select A.*,(select count(*) from 表二 where 投标=A.工程 and 投哪个='AA') 负责人(投标总COUNT),(select sum(资金) from 表二 where 投标=A.工程 and 投哪个='AA') 总资金SUM,(select count(*) from 表二 where 投标=A.工程 and 投标=A.工程 and 投哪个='AAA') 负责人2(COUNT),
    (select sum(资金) from 表二 where 投标=A.工程 and 投哪个='AAA') 总资
    from 表一 A
      

  2.   

    select 资金=sun(资金),(投标总COUNT)=count into  表3 from 表2
    group by 投标,投哪个
    go
    insert 表1 select 负责人(投标总COUNT),总资金SUM=资金,负责人2(COUNT)|总资金 from 表3
      
      

  3.   

    表二  
    投标      资金      投哪个
    --------------------------  
    A            100     AA  ------1
    A            230     AAA ------2
    C            123     CC
    B            234     BB
    A            234     AA --------3
    D            100     DD这里怎么三个A 投标?没看懂
      

  4.   

    才看明白,不过不知道你的负责人1与负责人2的是按什么划分开的?
    --给你个参考吧:
    --测试:
    create table 表一(工程 char(1),负责人1 varchar(6),负责人2 varchar(6))
    insert into 表一
    select 'A','AA','AAA'  union all
    select 'B', 'BB','BBB' union all
    select 'C',  'CC',  'CCC'union all
    select 'D',  'DD',  'DDD'create table 表二(投标 char(1),资金 int ,找哪家 varchar(6))
    insert into 表二
    select 'A',100,'AA'  union all
    select 'A',230,'AAA'  union all
    select 'C',123,'CC' union all
    select 'B',234,'BB'union all
    select 'A',234,'AA'  union all
    select 'D',100,'DD'--查询:select a.*,b.投标总数,b.总资金1,c.负责人2总数,c.总资金2 from 表一 a left join 
    (select 投标,投标总数=count(*),总资金1=sum(资金) from 表二
     where len(找哪家)=2
     group by 投标
    ) b on a.工程=b.投标
    left  join (
    select 投标,负责人2总数=count(*),总资金2=sum(资金) from 表二
     where len(找哪家)=3
     group by 投标
    ) c on a.工程=c.投标
    drop table 表一,表二--结果:
    /*
    工程   负责人1   负责人2   投标总数        总资金1        负责人2总数      总资金2        
    ---- ------ ------ ----------- ----------- ----------- ----------- 
    A    AA     AAA    2           334         1           230
    B    BB     BBB    1           234         NULL        NULL
    C    CC     CCC    1           123         NULL        NULL
    D    DD     DDD    1           100         NULL        NULL
    */
      

  5.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table_1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[table_1]if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table_2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[table_2]gocreate table table_1(工程 varchar(10)  not null primary key ,  負責人1  varchar(10)  null ,  負責人2  varchar(10)  null)create table table_2(投標 varchar(10) not null, 資金 int not null, 投哪個 varchar(10)  not null)goinsert into table_1
    select 'A',     'AA',    'AAA' union
    select 'b',     'BB',    'BBB' union 
    select 'C',     'CC',    'CCC' union 
    select 'D',     'DD',    'DDD'insert into table_2
    select 'A',  100,   'AA'  union all
    select 'A',  230,   'AAA' union all
    select 'C',  123,   'CC'  union all
    select 'B',  234,   'BB'  union all
    select 'A',  234,   'AA'  union all
    select 'D',  100,   'DD'
    goselect * from table_1
    /*
    工程   負責人1   負責人2
    A AA AAA
    b BB BBB
    C CC CCC
    D DD DDD
        
    */select * from table_2
    /*
    投標     資金    投哪個
    A 100 AA
    A 230 AAA
    C 123 CC
    B 234 BB
    A 234 AA
    D 100 DD*/--方法一
    select 工程,負責人1 as 負責人,負責人2,'負責人(投標總COUNT)'=(select count(*) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人1=投哪個 ), 總資金SUM=(select  sum(資金) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人1=投哪個 ),'負責人2(COUNT)'=(select isnull(count(*),0) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人2=投哪個 ), 總資金=(select isnull(sum(資金),0) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人2=投哪個 ) from table_1 a
    /*
    工程  負責人 負責人2  負責人(投標總COUNT)  總資金SUM  負責人2(COUNT)    總資金
    A      AA     AAA      2                334        1       230
    b      BB      BBB        1         234  0        0
    C      CC      CCC          1                123  0        0
    D      DD      DDD          1                100  0        0
    */ --方法二
     if exists (select * from tempdb..sysobjects where id = object_id('tempdb..#temp2'))
     drop table #temp2     
     goselect 投標,投哪個,count(*) as 負責人count, sum(資金) as 總資金 into #temp2 from table_2  group by 投標,投哪個select 工程,負責人1 as 負責人,負責人2,'負責人(投標總COUNT)'=(select 負責人count from #temp2 where a.工程=投標 and a.負責人1=投哪個 ), 總資金SUM=(select 總資金 from #temp2   where a.工程=投標 and a.負責人1=投哪個 ),'負責人2(COUNT)'=isnull((select 負責人count from #temp2  where a.工程=投標 and a.負責人2=投哪個),0), 總資金=isnull((select 總資金 from #temp2   where a.工程=投標 and a.負責人2=投哪個),0) from table_1 a
    /*
    工程  負責人 負責人2  負責人(投標總COUNT)  總資金SUM  負責人2(COUNT)    總資金
    A      AA     AAA      2                334        1       230
    b      BB      BBB        1         234  0        0
    C      CC      CCC          1                123  0        0
    D      DD      DDD          1                100  0        0
    */ 
      

  6.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table_1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[table_1]if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table_2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[table_2]gocreate table table_1(工程 varchar(10)  not null primary key ,  負責人1  varchar(10)  null ,  負責人2  varchar(10)  null)create table table_2(投標 varchar(10) not null, 資金 int not null, 投哪個 varchar(10)  not null)goinsert into table_1
    select 'A',     'AA',    'AAA' union
    select 'b',     'BB',    'BBB' union 
    select 'C',     'CC',    'CCC' union 
    select 'D',     'DD',    'DDD'insert into table_2
    select 'A',  100,   'AA'  union all
    select 'A',  230,   'AAA' union all
    select 'C',  123,   'CC'  union all
    select 'B',  234,   'BB'  union all
    select 'A',  234,   'AA'  union all
    select 'D',  100,   'DD'
    goselect * from table_1
    /*
    工程   負責人1   負責人2
    A AA AAA
    b BB BBB
    C CC CCC
    D DD DDD
        
    */select * from table_2
    /*
    投標     資金    投哪個
    A 100 AA
    A 230 AAA
    C 123 CC
    B 234 BB
    A 234 AA
    D 100 DD*/--方法一
    select 工程,負責人1 as 負責人,負責人2,'負責人(投標總COUNT)'=(select count(*) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人1=投哪個 ), 總資金SUM=(select  sum(資金) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人1=投哪個 ),'負責人2(COUNT)'=(select isnull(count(*),0) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人2=投哪個 ), 總資金=(select isnull(sum(資金),0) from table_2  group by 投標,投哪個 having a.工程=投標 and a.負責人2=投哪個 ) from table_1 a
    /*
    工程  負責人 負責人2  負責人(投標總COUNT)  總資金SUM  負責人2(COUNT)    總資金
    A      AA     AAA      2                334        1       230
    b      BB      BBB        1         234  0        0
    C      CC      CCC          1                123  0        0
    D      DD      DDD          1                100  0        0
    */ --方法二
     if exists (select * from tempdb..sysobjects where id = object_id('tempdb..#temp2'))
     drop table #temp2     
     goselect 投標,投哪個,count(*) as 負責人count, sum(資金) as 總資金 into #temp2 from table_2  group by 投標,投哪個select 工程,負責人1 as 負責人,負責人2,'負責人(投標總COUNT)'=(select 負責人count from #temp2 where a.工程=投標 and a.負責人1=投哪個 ), 總資金SUM=(select 總資金 from #temp2   where a.工程=投標 and a.負責人1=投哪個 ),'負責人2(COUNT)'=isnull((select 負責人count from #temp2  where a.工程=投標 and a.負責人2=投哪個),0), 總資金=isnull((select 總資金 from #temp2   where a.工程=投標 and a.負責人2=投哪個),0) from table_1 a
    /*
    工程  負責人 負責人2  負責人(投標總COUNT)  總資金SUM  負責人2(COUNT)    總資金
    A      AA     AAA      2                334        1       230
    b      BB      BBB        1         234  0        0
    C      CC      CCC          1                123  0        0
    D      DD      DDD          1                100  0        0
    */ --方法三
    select 工程,負責人1 as 負責人,負責人2,'負責人(投標總COUNT)'=(select count(*) from table_2  where a.工程=投標 and a.負責人1=投哪個 ), 總資金SUM=(select  sum(資金) from table_2  where  a.工程=投標 and a.負責人1=投哪個 ),'負責人2(COUNT)'=(select isnull(count(*),0) from table_2  where a.工程=投標 and a.負責人2=投哪個 ), 總資金=(select isnull(sum(資金),0) from table_2  where a.工程=投標 and a.負責人2=投哪個 ) from table_1 a
    /*
    工程  負責人 負責人2  負責人(投標總COUNT)  總資金SUM  負責人2(COUNT)    總資金
    A      AA     AAA      2                334        1       230
    b      BB      BBB        1         234  0        0
    C      CC      CCC          1                123  0        0
    D      DD      DDD          1                100  0        0
    */ 
      

  7.   

    create table 表一(工程 varchar(20),负责人1 varchar(20),负责人2 varchar(20) constraint pk_A primary key(工程))
    create table 表二(投标 varchar(20),资金 int,投哪个 varchar(20))
    ---
    insert into 表一 select 'A','AA','AAA'
    union select 'B','BB','BBB'
    union select'C','CC','CCC'
    union select'D','DD','DDD'--
    insert into 表二 select 'A','100','AA'
    union select 'A','230','AAA'
    union select 'C','123','CC'
    union select 'B','234','BB'
    union select 'A','234','AA'
    union select 'D','100','DD'--查询
    select a.*,b.投标总数[负责人(投标总COUNT)],b.总资金1[总资金SUM],isnull(c.负责人2总数,0)[负责人2(COUNT)],isnull(c.总资金2,0)[总资金] 
    from 表一 a left join 
    (select 投标,投标总数=count(*),总资金1=sum(资金) from 表二
     where exists(select * from 表一 where 表一.负责人1=表二.投哪个)
     group by 投标
    ) b on a.工程=b.投标
    left  join (
    select 投标,负责人2总数=count(*),总资金2=sum(资金) from 表二
     where exists(select * from 表一 where 表一.负责人2=表二.投哪个)
     group by 投标
    ) c on a.工程=c.投标
    --结果
    工程   负责人1   负责人2  负责人(投标总COUNT) 总资金SUM  负责人2(COUNT) 总资金         
    -------------------- -------------------- -------------------- -----------
    A      AA       AAA        2                 334         1           230
    B      BB       BBB        1                 234         0           0
    C      CC       CCC        1                 123         0           0
    D      DD       DDD        1                 100         0           0
      

  8.   

    晕,这么多啦,呵呵!凑热闹来
    --测试:
    create table 表一(工程 char(1),负责人1 varchar(6),负责人2 varchar(6))
    insert into 表一
    select 'A','AA','AAA'  union all
    select 'B', 'BB','BBB' union all
    select 'C',  'CC',  'CCC'union all
    select 'D',  'DD',  'DDD'create table 表二(投标 char(1),资金 int ,找哪家 varchar(6))
    insert into 表二
    select 'A',100,'AA'  union all
    select 'A',230,'AAA'  union all
    select 'C',123,'CC' union all
    select 'B',234,'BB'union all
    select 'A',234,'AA'  union all
    select 'D',100,'DD'--查询:select a.*,b.投标总数,b.总资金1,c.负责人2总数,c.总资金2 from 表一 a left join 
    (select 投标,投标总数=count(*),总资金1=sum(资金),找哪家 from 表二
      group by 投标,找哪家
    ) b on a.工程=b.投标 and a.负责人1=b.找哪家
    left  join (
     select 投标,负责人2总数=count(*),总资金2=sum(资金),找哪家 from 表二
     group by 投标,找哪家
    ) c on a.工程=c.投标 and a.负责人2=c.找哪家drop table 表一,表二--结果:
    工程   负责人1   负责人2   投标总数        总资金1        负责人2总数      总资金2        
    ---- ------ ------ ----------- ----------- ----------- ----------- 
    A    AA     AAA    2           334         1           230
    B    BB     BBB    1           234         NULL        NULL
    C    CC     CCC    1           123         NULL        NULL
    D    DD     DDD    1           100         NULL        NULL