有这样两个表COMPANYCID         Cname
1 公司A
2 公司B
3 公司C
4 公司D
5 公司E
count
id        cid        NUM
101101 1 10
101101 1 2
101102 1 15
1012 1 8
1015 1 16
1015 1 7
1015 1 8
10160101 1 9
10160102 1 5
10160102 1 19
101602 1 5
101101 2 5
101102 2 15
1012 2 8
1015 2 7
10160101 2 9
10160102 2 0
101602 2 3
.....有这样一个对应关系(id的前四位就能决定对应关系)
101101  a
101102 a
1012 a
1015 b
10160101 b
10160102 b
101602 b
.....但是这个对应关系现在不能进入数据库,需要根据上面两张表查询出如下的结果
Cname  a b
公司A       35           69 
公司B       38           19
这个查询怎么写效率比较高

解决方案 »

  1.   

    这个对应关系得在SQL语句中手工写
      

  2.   

    根据id及CID对应的a b项对num求和
      

  3.   

    根据CID及id对应的a b项对num求和
      

  4.   

    select cname ,
    (select   sum(num) as a
    from  count  
    where (id like '1011%' or id like '1012%') and cid=company.cid ) as a,
    (select   sum(num) as a
    from  count  
    where (id like '1015%' or id like '1016%') and cid=company.cid ) as b
    from company有更简单点的方法吗  或者能提高一下查询效率
      

  5.   

    select
       a.cname,
       sum(case when id like '1011%' or id like '1012%' then 1 else 0 end) as a,
       sum(case when id like '1015%' or id like '1016%' then 1 else 0 end) as b
    from
       company a,[count] b
    where 
       a.cid=b.cid 
    group by
       a.cname
      
      

  6.   


    declare @COMPANY table (CID int,Cname varchar(5))
    insert into @COMPANY
    select 1,'公司A' union all
    select 2,'公司B' union all
    select 3,'公司C' union all
    select 4,'公司D' union all
    select 5,'公司E'declare @count table (id varchar(10),cid int,NUM int)
    insert into @count
    select 101101,1,10 union all
    select 101101,1,2 union all
    select 101102,1,15 union all
    select 1012,1,8 union all
    select 1015,1,16 union all
    select 1015,1,7 union all
    select 1015,1,8 union all
    select 10160101,1,9 union all
    select 10160102,1,5 union all
    select 10160102,1,19 union all
    select 101602,1,5 union all
    select 101101,2,5 union all
    select 101102,2,15 union all
    select 1012,2,8 union all
    select 1015,2,7 union all
    select 10160101,2,9 union all
    select 10160102,2,0 union all
    select 101602,2,3
    select a.Cname,b.a,b.b from @COMPANY a left join (
    select cid,
    a=sum(case when left(id,4) between '1011' and '1012' then num else 0 end) ,
    b=sum(case when left(id,4) between '1015' and '1016' then num else 0 end)
    from @count group by cid) b on a.CID=b.cid where b.cid is not null
    /*
    Cname a           b
    ----- ----------- -----------
    公司A   35          69
    公司B   28          19
    */
      

  7.   


    select Cname,a=sum(case when left(id,4) <'1013' then num else 0 end) ,
    b=sum(case when left(id,4) >'1013' then num else 0 end)
    from @COMPANY a,@count c where a.CID=c.cid group by Cname/*
    Cname a           b
    ----- ----------- -----------
    公司A   35          69
    公司B   28          19
    */
      

  8.   

    select Cname,a=sum(case when left(id,4) <'1013' then num else 0 end) ,
    b=sum(case when left(id,4) >'1013' then num else 0 end)
    from @COMPANY a,@count c where a.CID=c.cid group by Cname
      

  9.   

    资源共享:
    SQL Server数据库的代码优化实例:
    http://database.51cto.com/art/201108/285253.htm