现有
表A
ID AccountName CarCode
-----------------------------------
1  重型汽车      1101,1102,1103,1104
2  中型汽车      1105,1106,1107,1108
3  轻型汽车      1109,1110,1111,1112表B
ID  CarCode CarName SalePrice SaleNum  Company
-----------------------------------------------
1   1101    东风101  85000     5        公司1
2   1102    跃进102  96000     3        公司2
3   1103    东风103  120000    2        公司1
4   1104    跃进104  135000    1        公司3
5   1105    长安101  84000     4        公司1
6   1106    长安102  97000     3        公司2
7   1107    北京101  78000     7        公司3  
8   1108    北京102  86000     5        公司2
9   1109    奇瑞101  28000     4        公司1
10  1110    奇瑞102  34000     6        公司3
11  1111    吉利101  25000     3        公司1
12  1112    吉利102  33000     4        公司2现要得到如下统计结果: 
序号   重型汽车   中型汽车   轻型汽车
公司1  665000    336000   187000
公司2  288000    721000   132000
公司3  135000    546000   204000请问查询语句该怎么写??

解决方案 »

  1.   

    Create table A
    (id int identity,AccountName varchar(10),carcode varchar(30))
    insert into a select  '重型汽车' ,     '1101,1102,1103,1104' 
    insert into a select  '中型汽车' ,    '1105,1106,1107,1108' 
    insert into a select  '轻型汽车' ,   '1109,1110,1111,1112' create table B
    (id int ,carcode varchar(10),carname varchar(10),saleprice int,salenum int,company varchar(10))
    insert into b select 1   ,'1101',    '东风101',  85000   ,  5 ,       '公司1' 
    insert into b select 2   ,'1102',    '跃进102',  96000   ,  3 ,       '公司2' 
    insert into b select 3   ,'1103',    '东风103',  120000  ,  2 ,      '公司1' 
    insert into b select 4   ,'1104',    '跃进104',  135000  ,  1 ,       '公司3' 
    insert into b select 5   ,'1105',    '长安101',  84000   ,  4 ,       '公司1' 
    insert into b select 6   ,'1106',    '长安102',  97000  ,   3 ,       '公司2' 
    insert into b select 7   ,'1107',    '北京101',  78000  ,   7 ,       '公司3'   
    insert into b select 8   ,'1108',    '北京102',  86000  ,   5 ,       '公司2' 
    insert into b select 9   ,'1109',    '奇瑞101',  28000  ,   4  ,      '公司1' 
    insert into b select 10  ,'1110',    '奇瑞102',  34000 ,    6  ,      '公司3' 
    insert into b select 11  ,'1111',    '吉利101',  25000 ,    3  ,      '公司1' 
    insert into b select 12  ,'1112',    '吉利102',  33000 ,    4  ,      '公司2' create view c 
    as
    select a.accountname,saleprice,salenum,company from a ,b
    where Charindex(b.carcode,a.carcode)>0select * from cdeclare @sql varchar(1000)
    set @sql='select company '
    select @sql=@sql+',sum(case when accountname='''+accountname+''' then salenum*saleprice else 0 end) as '+accountname
    from (select distinct accountname from c ) a
    set @sql=@sql+' from c group by company'
    exec(@sql)company    轻型汽车        中型汽车        重型汽车
    ---------- ----------- ----------- -----------
    公司1        187000      336000      665000
    公司2        132000      721000      288000
    公司3        204000      546000      135000(3 行受影响)
      

  2.   

    试写一下,
    declare @a char(100),@b char(100),@c char(100),@d char(100),@e char(100),@f char(100),@g char(100),@h char(100),@i char(100)
    select @a= sum(saleprice * salenum)  from B where company='公司1' and carcode in ('1101','1102','1103','1104')
    select @b= sum(saleprice * salenum)  from B where company='公司1' and carcode in ('1105','1106','1107','1108')
    select @c= sum(saleprice * salenum)  from B where company='公司1' and carcode in ('1109','1110','11011','1112')
    select @d= sum(saleprice * salenum)  from B where company='公司2' and carcode in ('1101','1102','1103','1104')
    select @e= sum(saleprice * salenum)  from B where company='公司2' and carcode in ('1105','1106','1107','1108')
    select @f= sum(saleprice * salenum)  from B where company='公司2' and carcode in ('1109','1110','11011','1112')
    select @g= sum(saleprice * salenum)  from B where company='公司3' and carcode in ('1101','1102','1103','1104')
    select @h= sum(saleprice * salenum)  from B where company='公司3' and carcode in ('1105','1106','1107','1108')
    select @i= sum(saleprice * salenum)  from B where company='公司3' and carcode in ('1109','1110','11011','1112')
    select '公司1'as company,@a as 重型汽车,@b as 中型汽车,@c as 中型汽车 union all
    select '公司2'as company,@d as 重型汽车,@e as 中型汽车,@f as 中型汽车 union all
    select '公司3'as company,@g as 重型汽车,@h as 中型汽车,@i as 中型汽车
      

  3.   

    --静态SQL,不需要临时表,视图.create table A(ID int,AccountName varchar(10),CarCode varchar(50))
    insert into A values(1,  '重型汽车',      '1101,1102,1103,1104') 
    insert into A values(2,  '中型汽车',      '1105,1106,1107,1108') 
    insert into A values(3,  '轻型汽车',      '1109,1110,1111,1112')
    create table B(ID int, CarCode varchar(4),CarName varchar(10),SalePrice int,SaleNum int, Company varchar(10))
    insert into B values(1 ,  '1101',    '东风101',  85000  ,   5  ,      '公司1') 
    insert into B values(2 ,  '1102',    '跃进102',  96000  ,   3  ,      '公司2') 
    insert into B values(3 ,  '1103',    '东风103',  120000 ,   2  ,      '公司1') 
    insert into B values(4 ,  '1104',    '跃进104',  135000 ,   1  ,      '公司3') 
    insert into B values(5 ,  '1105',    '长安101',  84000  ,   4  ,      '公司1') 
    insert into B values(6 ,  '1106',    '长安102',  97000  ,   3  ,      '公司2') 
    insert into B values(7 ,  '1107',    '北京101',  78000  ,   7  ,      '公司3')   
    insert into B values(8 ,  '1108',    '北京102',  86000  ,   5  ,      '公司2') 
    insert into B values(9 ,  '1109',    '奇瑞101',  28000  ,   4  ,      '公司1') 
    insert into B values(10,  '1110',    '奇瑞102',  34000  ,   6  ,      '公司3') 
    insert into B values(11,  '1111',    '吉利101',  25000  ,   3  ,      '公司1') 
    insert into B values(12,  '1112',    '吉利102',  33000  ,   4  ,      '公司2')
    goselect company , 
       sum(case accountname when '重型汽车' then saleprice else 0 end) '重型汽车',
       sum(case accountname when '中型汽车' then saleprice else 0 end) '中型汽车',
       sum(case accountname when '轻型汽车' then saleprice else 0 end) '轻型汽车'
    from
    (
      select B.Company , b.SalePrice , A.AccountName from B,A where charindex(','+B.carcode+',' , ',' + A.carcode + ',') > 0
    ) t
    group by companydrop table A,B/*
    company    重型汽车        中型汽车        轻型汽车        
    ---------- ----------- ----------- ----------- 
    公司1        205000      84000       53000
    公司2        96000       183000      33000
    公司3        135000      78000       34000(所影响的行数为 3 行)
    */
      

  4.   

    --上面没*数量--静态SQL,不需要临时表,视图. create table A(ID int,AccountName varchar(10),CarCode varchar(50)) 
    insert into A values(1,   '重型汽车',       '1101,1102,1103,1104 ')  
    insert into A values(2,   '中型汽车',       '1105,1106,1107,1108 ')  
    insert into A values(3,   '轻型汽车',       '1109,1110,1111,1112 ') 
    create table B(ID int, CarCode varchar(4),CarName varchar(10),SalePrice int,SaleNum int, Company varchar(10)) 
    insert into B values(1 ,   '1101 ',     '东风101 ',  85000  ,   5  ,       '公司1 ')  
    insert into B values(2 ,   '1102 ',     '跃进102 ',  96000  ,   3  ,       '公司2 ')  
    insert into B values(3 ,   '1103 ',     '东风103 ',  120000 ,   2  ,       '公司1 ')  
    insert into B values(4 ,   '1104 ',     '跃进104 ',  135000 ,   1  ,       '公司3 ')  
    insert into B values(5 ,   '1105 ',     '长安101 ',  84000  ,   4  ,       '公司1 ')  
    insert into B values(6 ,   '1106 ',     '长安102 ',  97000  ,   3  ,       '公司2 ')  
    insert into B values(7 ,   '1107 ',     '北京101 ',  78000  ,   7  ,       '公司3 ')    
    insert into B values(8 ,   '1108 ',     '北京102 ',  86000  ,   5  ,       '公司2 ')  
    insert into B values(9 ,   '1109 ',     '奇瑞101 ',  28000  ,   4  ,       '公司1 ')  
    insert into B values(10,   '1110 ',     '奇瑞102 ',  34000  ,   6  ,       '公司3 ')  
    insert into B values(11,   '1111 ',     '吉利101 ',  25000  ,   3  ,       '公司1 ')  
    insert into B values(12,   '1112 ',     '吉利102 ',  33000  ,   4  ,       '公司2 ') 
    go select company ,  
       sum(case accountname when  '重型汽车 ' then SalePrice * SaleNum else 0 end)  '重型汽车 ', 
       sum(case accountname when  '中型汽车 ' then SalePrice * SaleNum else 0 end)  '中型汽车 ', 
       sum(case accountname when  '轻型汽车 ' then SalePrice * SaleNum else 0 end)  '轻型汽车 ' 
    from 

      select B.Company , B.SalePrice , B.SaleNum, A.AccountName  from B,A where charindex( ','+B.carcode+ ',' ,  ',' + A.carcode +  ',')  > 0 
    ) t 
    group by company drop table A,B 
      

  5.   

    MD,CSDN自动把'1101'改为'1101  ',害我动态语句搞了半天没结果.这帮改版的人真是些猪.
      

  6.   

    --静态SQL,不需要临时表,视图. 
    --动态SQL
    create table A(ID int,AccountName varchar(10),CarCode varchar(50)) 
    insert into A values(1,   '重型汽车',       '1101,1102,1103,1104')  
    insert into A values(2,   '中型汽车',       '1105,1106,1107,1108')  
    insert into A values(3,   '轻型汽车',       '1109,1110,1111,1112') 
    create table B(ID int, CarCode varchar(4),CarName varchar(10),SalePrice int,SaleNum int, Company varchar(10)) 
    insert into B values(1 ,   '1101',     '东风101 ',  85000  ,   5  ,       '公司1')  
    insert into B values(2 ,   '1102',     '跃进102 ',  96000  ,   3  ,       '公司2')  
    insert into B values(3 ,   '1103',     '东风103 ',  120000 ,   2  ,       '公司1')  
    insert into B values(4 ,   '1104',     '跃进104 ',  135000 ,   1  ,       '公司3')  
    insert into B values(5 ,   '1105',     '长安101 ',  84000  ,   4  ,       '公司1')  
    insert into B values(6 ,   '1106',     '长安102 ',  97000  ,   3  ,       '公司2')  
    insert into B values(7 ,   '1107',     '北京101 ',  78000  ,   7  ,       '公司3')    
    insert into B values(8 ,   '1108',     '北京102 ',  86000  ,   5  ,       '公司2')  
    insert into B values(9 ,   '1109',     '奇瑞101 ',  28000  ,   4  ,       '公司1')  
    insert into B values(10,   '1110',     '奇瑞102 ',  34000  ,   6  ,       '公司3')  
    insert into B values(11,   '1111',     '吉利101 ',  25000  ,   3  ,       '公司1')  
    insert into B values(12,   '1112',     '吉利102 ',  33000  ,   4  ,       '公司2') 
    go 
    declare @sql varchar(8000)
    set @sql = 'select Company '
    select @sql = @sql + ' , sum(case AccountName when ''' + AccountName + ''' then SalePrice * SaleNum else 0 end) [' + AccountName + ']'
    from (select distinct AccountName from (select B.Company , B.SalePrice , B.SaleNum, A.AccountName from B,A where charindex(','+B.carcode+',' , ',' + A.carcode + ',') > 0) t) as a
    set @sql = @sql + ' from (select B.Company , B.SalePrice , B.SaleNum, A.AccountName from B,A where charindex('',''+B.carcode+'','' , '','' + A.carcode + '','') > 0) t group by Company '
    exec(@sql) drop table A,B /*
    Company    轻型汽车        中型汽车        重型汽车        
    ---------- ----------- ----------- ----------- 
    公司1        187000      336000      665000
    公司2        132000      721000      288000
    公司3        204000      546000      135000
    */