-1

I'm using Postgres, and my dataset is an orders table. I'm trying to learn how to show the total amount of orders per month, per user (assuming there's only one user in this case).

My dataset:

id, user_id, total, created_at

My current query:

SELECT
       DATE_TRUNC('month',created_at)
         AS  total_orders,
       COUNT(id) AS count
FROM orders
WHERE id = 1
GROUP BY DATE_TRUNC('month',created_at);

This returns:

Total orders | count
2021-01-01 00:00:00, 1

However, here's the tricky part (to me at least) - i am using this to generate some graphs. Specifically, the last 12 months. So i would like the query to return the last 12 months, and include 0 for when no orders were created. So this is expected output:

Total orders | count
2021-04-01 00:00:00, 0
2021-03-01 00:00:00, 0
2021-02-01 00:00:00, 0
2021-01-01 00:00:00, 1
2020-12-01 00:00:00, 0
2020-11-01 00:00:00, 0
2020-10-01 00:00:00, 0
2020-09-01 00:00:00, 0
2020-08-01 00:00:00, 0
2020-07-01 00:00:00, 0
2020-06-01 00:00:00, 0
2020-05-01 00:00:00, 0
2020-04-01 00:00:00, 0

How can I accomplish this? The ultimate goal is to be super lightweight so that it's not resource intensive and fast.

maciejwww
  • 103
  • 5
estransconder
  • 23
  • 1
  • 4
  • Please add a [mre] to your qiestion – nbk Apr 10 '21 at 23:59
  • @nbk its already there. read rather than downvote and comment meaningless stuff. – estransconder Apr 11 '21 at 01:01
  • a ]mre] is clearly, that the result set expected, must have a source table(s) as it is stated in the provided link. – nbk Apr 11 '21 at 07:54
  • as per my original message, its in the problem. It's Total_orders. Already solved. Thanks. @nbk – estransconder Apr 11 '21 at 07:57
  • i have a similar solution, and wanted to test it, but i really don't wanted to create data as this is really your part – nbk Apr 11 '21 at 08:09
  • @nbk, ah i misunderstood you. My apologies. Feel free to post. – estransconder Apr 11 '21 at 08:45
  • 1
    Hii, and welome to the forum! I think that your question is fair enough - although some people seem to be too quick off the mark to downvote and/or vote to close! As a New Contributor, you should have been cut some slack. Just a couple of pointers: 1) **Always include your server version -with your question, either as a tag or clearly at the beginnning 2) When you provide table data, you should also include it as a fiddle - i.e with DDL and DML - db.fiddle.uk is generally considered the best - this helps those trying to help you! – Vérace Apr 11 '21 at 09:28
  • Your answer contains a link which is fine! However, it's normally considered good form to put the relevant code extracts into the answer itself. This is probably whey it was downvoted. You can mark your own answer as correct (after a certain time?) - this is also fine, just no points! :-) p.s. I do wish that those who downvote would leave a reason - how else are people to learn where they're goig wrong and how to improve?
  • – Vérace Apr 11 '21 at 09:29
  • Hi @Vérace Thank you!!! Noted for future questions <3!! – estransconder Apr 12 '21 at 05:06