国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

sqlServer中 分類報表sql技巧 ,很實用

2024-07-21 02:05:24
字體:
來源:轉載
供稿:網友

有表 tablea 如下:-----------------------------------------------------id,    type ,   countnumber ,   countdate1 a 10  2005-02-01 21:23:342 b 8  2005-02-01 21:23:343 c 5  2005-02-01 21:23:344 b 4  2005-02-01 11:23:345 c 5  2005-02-01 22:23:346 a 11  2005-02-02 12:23:347 b 9  2005-02-02 14:23:348 c 8  2005-02-02 17:23:349 a 15  2005-02-02 19:23:3410 c 6  2005-02-02 04:23:3411 a 7  2005-02-03 1:23:3412 b 11  2005-02-03 2:23:3413 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 tableagroup by convert(char(10),countdate,20)

只用 1秒 不到!  唉! 厲害厲害!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山东省| 肇东市| 阿鲁科尔沁旗| 邯郸市| 合江县| 新源县| 合川市| 巍山| 安阳市| 宁明县| 荔波县| 定兴县| 洮南市| 高清| 仙游县| 龙川县| 宁陵县| 克什克腾旗| 新郑市| 固安县| 双桥区| 阳泉市| 荔波县| 珲春市| 离岛区| 沁源县| 三江| 平遥县| 山丹县| 西青区| 新津县| 荆门市| 彩票| 平南县| 中牟县| 凭祥市| 德保县| 衡阳县| 比如县| 新闻| 东港市|