sqlServer中 分類報表sql技巧

2021-04-13 12:40:26 字數 1622 閱讀 4538

有表 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秒 不到!  唉! 厲害厲害!

OC中分類的使用

在oc中,如果想對某個類進行方法的擴充,又不想改變原有累的方法和成員變數,可以使用分類 分類 category 基本用途 格式 inte ce 類名 分類名稱 方法宣告 end implementation 類名 分類名稱 方法實現 end 好處 給系統自帶的類新增分類 注意 示例 首先 建立乙個 ...

機器學習中分類問題

regression與線性回歸 linear regression 線性回歸 linear regression 損失函式 loss function 我們要找到最好的權重 引數 怎麼去衡量 最好 定義損失函式 最小化損失函式 梯度下降 gradient descend 逐步最小化損失函式的過程 如...

SQL Server 報表服務

報表服務 reporting services,ssrs 提供了各種現成可用的工具和服務,幫助資料庫管理員建立,部署和管理單位的報表,並提供了能夠擴充套件和自定義報表功能的程式設計功能。每乙個事務都有生命週期,報表也是如此。每個報表的的生存週期都包括報表建立 報表管理和報表傳輸三個活動過程。ssrs...