作者grence (多想兩分鐘 = =")
看板Database
標題Re: [SQL ] 請問如何做到Excel中的countif功能
時間Tue Dec 23 14:25:30 2008
※ 引述《Killingm (殺天使)》之銘言:
: 使用的是SQL server 2008
: Microsoft SQL Server Management Studio 10.0.1600.22
: test:
: no a1 a2 a3 a4 a5
: 1 100 99 100 95 98
: 2 92 98 99 100 90
: 3 99 95 94 92 100
: 4 98 97 100 96 90
: 5 91 93 91 100 100
with tmp as(
select a1 from test
union all
select a2 from test
union all
select a3 from test
union all
select a4 from test
union all
select a5 from test
)
select a1, count(1)
from tmp
group by a1
: 我統計a1-a5 x 1-5 間25個數字,每一個數字出現的次數。
: 做出類似excel countif(a11:a55)的功能。
: 目前只有想到兩個土法煉鋼的方法。
: 第一個 select出來後丟入陣列去計算,
: 但是實際資料量比例子大很多,大約5萬筆,效率有點差,嘗試分段丟好像也沒改善多少。
: 第二個 針對每一個結果寫一個運算式加總。
: select count(*) from test where a1=100
: ...
: select count(*) from test where a5=100
: 再把結果加總起來,但是這樣變成要寫一百多個類似的區塊(其實for迴圈只有一個)
: 但是這樣好像有點笨...
: 不知道是否有人有比較方便的方法~謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.61.172.157
推 Killingm:感謝..我完全不知道有這招XD 謝謝 ^_^ 12/23 20:03
→ grence:) 12/24 10:52