Anda memerlukan gabungan luar di tabel kalender. Sesuatu seperti
SELECT c.cal_date, coalesce(t1.price, 0) as total
FROM calendar c
LEFT JOIN (SELECT b.purchase_date::date, sum(price) as price
FROM order_products a
INNER JOIN order_saved b
ON a.order_id = b.id
AND b.purchase_date BETWEEN '2011-09-16 23:59' AND '2011-10-16 23:59'
AND b.status > 2
AND a.usr_id = 'XXXX'
GROUP BY b.purchase_date) t1 on t1.purchase_date = c.cal_date
WHERE cal_date BETWEEN '2011-09-16 23:59' AND '2011-10-16 23:59'
ORDER BY c.cal_date
Anda akan mendapatkan jawaban yang lebih baik jika Anda memposting DDL (CREATE TABLE
) pernyataan, dan minimal INSERT
pernyataan yang memberi kita cukup data untuk benar-benar menguji jawaban kita. (Edit pertanyaan Anda dan tempel pernyataan DDL dan INSERT.)