我有一个SELECT
语句,它计算实例数,然后保存在变量中。它有一个HAVING
子句,可以执行SUM
和COUNT
。但是,由于您必须拥有GROUP BY
才能使用 , select 语句将返回4行而不是总数为4的行。不会将计数保存为变量为4但是为1显然不是我需要的,所以我正在寻找替代方法。
select count(distinct p1.community)
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
group By
p1.community
having
sum(p1.status_1) / count(p1.control_code) >= .16;
答案 0 :(得分:1)
这是一个合理的选择:
select count(*)
from (
select p1.community , sum(p1.status_1) / count(p1.control_code) SomeColumn
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
Group By
p1.community
) A
where A.SomeColumn >= .16;