我有一个简单的查询,我正在尝试调试,因为它给了我不正确的结果:
WITH a AS (
SELECT
id,
reference_date,
date,
COALESCE(failed_sum, 0) AS failed_sum,
COALESCE(succ_sum, 0) AS succ_sum,
COALESCE(failed_sum, 0) + COALESCE(succ_sum, 0) AS total_sum
FROM transactions
WHERE id = 'ABC'
)
SELECT id,
SUM(failed_sum) / SUM(total_sum) FILTER (WHERE date >= DATE_ADD('day', -30, reference_date) AND date < reference_date) AS ratio
FROM a
GROUP BY 1;
这产生了 6 的值,而我预期它是 <1 as it's a ratio of failed over total.
但是,当我像这样更改查询时:
WITH a AS (
SELECT
id,
reference_date,
date,
COALESCE(failed_sum, 0) AS failed_sum,
COALESCE(succ_sum, 0) AS succ_sum,
COALESCE(failed_sum, 0) + COALESCE(succ_sum, 0) AS total_sum
FROM transactions
WHERE id = 'ABC'
)
SELECT id,
SUM(failed_sum) / SUM(total_sum) AS ratio
FROM a
WHERE date >= DATE_ADD('day', -30, reference_date) AND date < reference_date
GROUP BY 1;
我得到了预期的结果 0.5。我预计这两个查询是相同的,但我确实发现两者之间的执行计划存在一些差异。我在这里遗漏了一些明显的东西吗?
提前致谢!
这里的区别在于你的第一个求和表达式:
SUM(failed_sum) / SUM(total_sum) FILTER (WHERE date >= DATE_ADD('day', -30, reference_date) AND date < reference_date) AS ratio
将包括整个表中每个id
的
所有记录。 表达式的分母只会对日期具有特定值的
total_sum
值求和。
另一方面,第二个查询:
SELECT id,
SUM(failed_sum) / SUM(total_sum) AS ratio
FROM a
WHERE date >= DATE_ADD('day', -30, reference_date) AND date < reference_date
GROUP BY 1;
实际上会删除不符合日期要求的记录。 这意味着对于许多
SUM(failed_sum)
组来说,第一个 id
可能会更少,因为一些记录会在求和发生之前被删除。 分母值可能相同,但这并不重要,因为商与第一个查询不一致。
至于你想要哪个版本,就看你的逻辑了。