Du skal bruge en ydre sammenføjning på kalenderbordet. Noget lignende
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
Du får bedre svar, hvis du sender DDL'en (CREATE TABLE
) sætninger og minimal INSERT
udsagn, der giver os nok data til rent faktisk at teste vores svar. (Rediger dit spørgsmål, og indsæt DDL- og INSERT-sætningerne.)