How to Calculate Monthly Sales Contribution to Yearly Sales in SQL?

markwilliams21's profile image markwilliams21 posted 1 month ago in General Permalink

Hi everyone,

I have a table named orders with the following columns:

  • orderdate (Date of the order)
  • sales (Sales amount for the order)

I am looking to write a query that will help me calculate the sales at a monthly level and show how each month's sales contribute to the yearly sales as a percentage.

Here's what I'm aiming to achieve in the output:

  • year (The year of the sales)
  • month (The month of the sales)
  • monthly_sales (Total sales for that month)
  • sales_contribution (Contribution of the month's sales to the yearly sales as a percentage)

Could someone help me with the SQL query to achieve this?

Thanks in advance!

TTSneko's profile image TTSneko posted 1 month ago Permalink

What the best approach is depends on the context and surroundings you're operating in. This is not StackOverflow or some other "need help with my homework" forum.

You did not even mention why you require this to be done in the database and not in a frontend. Nor did you specify the basics of your DB (data formats, etc.) or tell us what you already TRIED to do, etc. etc. etc.

And what has all this got to do with HeidiSQL?

Please login to leave a reply, or register at first.