CTE ответ ...
WITH
step1 AS
(
SELECT
SUM(value1) AS interimValue1
SUM(CASE value2 > 0 THEN 1 ELSE 0 END) AS interimValue2,
<other fields>
FROM
<a query>
)
SELECT
interimValue / CASE interimValue2 = 0 THEN 1 ELSE interimValue2 END
FROM
step1
Версия SubQuery ...
SELECT
interimValue / CASE interimValue2 = 0 THEN 1 ELSE interimValue2 END
FROM
(
SELECT
SUM(value1) AS interimValue1
SUM(CASE value2 > 0 THEN 1 ELSE 0 END) AS interimValue2,
<other fields>
FROM
<a query>
)
AS step1
Или, более конкретно, к указанному вами SQL-запросу, но не к общему случаю ...
SUM(value1) / COALESCE(SUM(CASE WHEN value2 > 0 THEN 1 ELSE NULL END), 1)
(Возвращение NULL, где вы возвращаете 0, позволяет COALESCE заменитьNULL с 1 с.)