Postgres SQL, as run
select sum(case when tags -> $1::text is not null then effective_cost else 0 end) / nullif(sum(effective_cost), 0) as tagged_ratio,
sum(case when tags -> $1::text is not null then effective_cost else 0 end) as tagged_effective_cost,
sum(effective_cost) as total_effective_cost
from focus.cost_and_usage
where charge_period_start >= $2::timestamptz
and charge_period_end < $3::timestamptz
and effective_cost > 0
and provider_name = $4::textParameters: $1 = tag_key, $2 = period_start, $3 = period_end, $4 = provider_name
Original FOCUS SQL
SELECT
SUM(CASE
WHEN JSON_CONTAINS_PATH(tags, 'one', '$.?')
THEN EffectiveCost
ELSE 0
END) / SUM(EffectiveCost) * 100 AS TaggedPercentage
FROM focus_data_table
WHERE ChargePeriodStart >= ? and ChargePeriodEnd < ?
AND EffectiveCost > 0
AND ProviderName = ?