Fill in missing rows when aggregating over multiple fields in Postgres
问题 I am aggregating sales for a set of products per day using Postgres and need to know not just when sales do happen, but also when they do not for further processing. SELECT sd.date, COUNT(sd.sale_id) AS sales, sd.product FROM sales_data sd -- sales per product, per day GROUP BY sd.product, sd.date ORDER BY sd.product, sd.date This produces the following: date | sales | product ------------+-------+------------------- 2017-08-17 | 10 | soap 2017-08-19 | 2 | soap 2017-08-20 | 5 | soap 2017-08