How would I improve my "algorithm" for figuring out my average spending on food per month?
I would first calculate the sum per month, then calculate the average from that:
select avg(amount) as avg_amount_per_month
from (
  SELECT date_trunc('month', &quot;purchase time&quot;), SUM(&quot;amount in EUR&quot;) as amount
  FROM personal.&quot;food purchases&quot; 
  WHERE &quot;purchase time&quot; &gt;= now() - INTERVAL '3 months'
  group by date_trunc('month', &quot;purchase time&quot;)
) t
;

The inner query will return the amount for each month of the chose interval (yielding three rows), and the outer query will then calculate the average from that.