作者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
1F:推 Killingm:感谢..我完全不知道有这招XD 谢谢 ^_^ 12/23 20:03
2F:→ grence:) 12/24 10:52