【发布时间】:2011-05-12 10:42:34
【问题描述】:
我有一个列名 state(int),其中根据逻辑只插入 0 和 1,我想要一个计数查询,它可以在单个查询中计算表中有多少个 0 和多少个 1喜欢:
从productDetail中选择Count(state)......
【问题讨论】:
标签: asp.net sql database count
我有一个列名 state(int),其中根据逻辑只插入 0 和 1,我想要一个计数查询,它可以在单个查询中计算表中有多少个 0 和多少个 1喜欢:
从productDetail中选择Count(state)......
【问题讨论】:
标签: asp.net sql database count
select
sum(state) countOfOnes,
sum(decode(state, 0, 1, 0)) countOfZeros
from
productDetail
;
或
select
sum(state) countOfOnes,
count(*) - sum(state) countOfZeros
from
productDetail
;
或
select
state,
count(*) over (partition by state order by state) countOfState
from
productDetail
;
前两个示例将返回一行两列:
countOfOnes countOfZeros
=========================
154 21
第三个示例将返回两行两列,每个状态一行。
state countOfState
=========================
0 21
1 154
【讨论】:
另一种变体:
select
count(*) countOverall,
sum(state) countOfOnes,
count(*) - sum(state) countOfZeroes
from
productDetail;
【讨论】:
select count(state) from productDetail Where state = 0
select count(state) from productDetail Where state = 1
【讨论】: