1

我有一个SELECT语句计算实例数,然后保存在一个变量中。它有一个执行 a和 a的HAVING子句。但是,由于您必须拥有 a才能使用having,因此select语句返回 4 行,它们是 1 而不是总数为 4。这不会将计数保存到变量中,而是保存为 1,这显然不是什么我需要,所以我正在寻找替代工作。SUMCOUNTGROUP BY

    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;
4

1 回答 1

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;
于 2013-11-04T19:02:35.310 回答