我手动执行以下示例。我想要的是%RespCounty
列的值,是将响应者的总和除以县的总和
当我使用它时,我得到一个错误:
无法对包含聚合或子查询的表达式执行聚合功能。
我正在使用sql 2016:
WITH CTE --test (county, masterleadtype,cnt)
AS(
select county,
SUM(CASE WHEN masterleadtype = 'responder' THEN SUM(cnt) ELSE 0 END) AS resp,
SUM(CASE WHEN masterleadtype = 'non responder' THEN SUM(cnt) END) AS non
from [RespondPercent]
group by county
)
select county, resp/(resp + non) as resp,
non/(resp + non) as nonresp
from cte