有这样两个表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 公司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
这个查询怎么写效率比较高
解决方案 »
- 请教个SQL写法问题,顺便再BS下CSDN的重复重复重复重复登录问题,登了再登,再登再再登,受不了拉
- excel 与 Sqlserver 之间的数据的导入导出
- 請教,我想讓我寫的存儲過程和觸發器讓別人看不到或不能改,最好是看不到,如何實現,我記得可以實現的,但不記得如何弄了。請教各位大俠。
- 如果存储由SQL语句得出的字段值
- 怎样安装 sql server 全文检索 组建啊??
- 请问两个外键可否对应同一个主键的问题,谢谢
- [求助]请教截断日志,收缩日志,删除日志文件的区别。
- 看看这个分页存储过程那儿出错了
- sql 查询语句
- 怎样把"数字"和"字符串"分开?
- 链接服务器 "(null)" 的 OLE DB 访问接口
- 求优化 sql语句
(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有更简单点的方法吗 或者能提高一下查询效率
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
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
*/
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
*/
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
SQL Server数据库的代码优化实例:
http://database.51cto.com/art/201108/285253.htm