I want to produce a list of sales (in postgres) grouped by day of the week and For each day of the week, get the number of sales and total profit. My coalesce doesnot seem to be working.
SELECT
to_char(order_time,'Dy'::TEXT) AS day,
coalesce(COUNT(menu_item_id),0) AS qty_sold,
coalesce(SUM(price - cost) ,0)AS total_profit
from sales_order AS s
inner join order_item as o on o.sales_order_id=s.id
inner join menu_item AS m on m.id=o.menu_item_id
where s.order_time > date_trunc('week', now())::date - 5
group by day
order by day asc
It sounds like you are missing dates from your report when there were no sales on those dates.
Use
generate_series()to create the dates of the report, and then left join into your orders: