聚合时过滤结果有关系吗?

Does it matter to filter results when doing aggregation?

我想获取我 orders_summary table 中每一天的销售额。

orders_summary table 列:id,日期,金额,sku_id

产品 table 列:id、sku

目前我的每日销售额是这样的:

SELECT
    MAX(CASE WHEN os.date = '01/01/2022' THEN COALESCE(amount,0)::INT ELSE 0 END) AS orders_1,
    MAX(CASE WHEN os.date = '01/02/2022' THEN COALESCE(amount,0)::INT ELSE 0 END) AS orders_2
FROM products AS p
LEFT JOIN orders_summary AS os ON p.id = os.sku_id
WHERE p.id = '1'
GROUP BY p.id;

在我的 where 子句中添加 AND date BETWEEN '01/01/2022' AND '01/02/2022' 很重要吗?

绝对是。想象一下 table 中有 10 年的数据,而您只对两天的数据感兴趣。您必须在 执行 group by.

之前使用限制行数的 where 子句(在本例中减少到 0.05%)