表A
UnitCode   UnitName TargetCode TargetValue         
23         北京公司     AAA        2222    
24         上海公司     BBB        3333       
25         深圳公司     CCC        4444
26         广州公司     DDD        5555   
27         加拿大公司   EEE        6666
28         加拿大公司   AAA        1234
表B
TargetCodeName                                                                                                                       
AAA       始发收入      
BBB       销售收入         
CCC       其他收入          
DDD       公里收入           
EEE       货机收入 
新出来的表后面的列根据B表Name来的
单位名称  始发收入 销售收入 其他收入 公里收入 货机收入  
深圳公司     0       0      4444      0          0 
北京公司   2222      0         0      0          0 
上海公司     0    3333         0      0          0 
广州公司     0       0         0    5555         0 
加拿大公司  1234     0         0      0       6666这个怎么实现

解决方案 »

  1.   

    select UnitName,始发收入, 销售收入, 其他收入, 公里收入, 货机收入 from A left join B on A.TargetCode=B.TargetCode
      

  2.   

    --建立测试环境
    set nocount on
    create table tableA(UnitCode varchar(20),UnitName varchar(20),TargetCode varchar(20),TargetValue int)
    insert into tableA select '23','北京公司','AAA','2222'
    insert into tableA select '24','上海公司','BBB','3333'
    insert into tableA select '25','深圳公司','CCC','4444'
    insert into tableA select '26','广州公司','DDD','5555'
    insert into tableA select '27','加拿大公司','EEE','6666'
    insert into tableA select '28','加拿大公司','AAA','1234'
    go
    create table tableB(TargetCode varchar(20),Name varchar(20))
    insert into tableB select 'AAA','始发收入'
    insert into tableB select 'BBB','销售收入'
    insert into tableB select 'CCC','其他收入'
    insert into tableB select 'DDD','公里收入'
    insert into tableB select 'EEE','货机收入'
    go
     
    declare @sql varchar(8000)
    set @sql='select unitname'
    select @sql=@sql+',sum(case when targetcode='''+targetcode+''' then targetvalue else 0 end)['+
    Name+']' from tableB
    set @sql=@sql+' from tableA group by unitname'exec(@sql)
    --删除测试环境
    drop table tableB
    drop table tableA
     set nocount off/*
    unitname             始发收入        销售收入        其他收入        公里收入        货机收入
    -------------------- ----------- ----------- ----------- ----------- -----------
    北京公司                 2222        0           0           0           0
    广州公司                 0           0           0           5555        0
    加拿大公司                1234        0           0           0           6666
    上海公司                 0           3333        0           0           0
    深圳公司                 0           0           4444        0           0
    */
      

  3.   

    那后面能跟条件?
    例如我只想看UnitCode 为24的
    等你回
      

  4.   

    declare @sql varchar(8000)
    set @sql='select unitname'
    select @sql=@sql+',sum(case when targetcode='''+targetcode+''' then targetvalue else 0 end)['+
        Name+']' from tableB
    set @sql=@sql+' from tableA where 你的条件  group by unitname'
      

  5.   

    很多都是case when then和聚合函数的联合操作
    比如说sum+case、max+case等等