Skip to content
Advertisement

Decluttering a SQL query

For a practice project i wrote the following query and i was wondering if there is way to make it more efficient than writing everything 12 times like a for loop for sql.

EDIT

I was given the 3 tables represented on the following database schema , and im trying to create a table with the total sales of dairy every month, the monthly market share of the dairy products and the difference between the 2020 monthly market share and the 2019 monthly market share (the year change colunm)

There is also an aritmethic error somewhere, when checking the project i get the following message ResultSet does not contain the correct numeric values! and im at my wits end looking for it butmy priority is to decluter the query.

Advertisement

Answer

Your error message tells me that you are trying to run this from a reporting tool or a host language.

It also makes no sense to put the data into separate tables by years.

SQL is a declarative language that works with data as sets.

Instead of pushing the results into table temp, try writing a query like this:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement