Skip to content
Advertisement

How to have MySQL return multiple column of values based on a different benchmark?

I have a MySQL database table (sales) consisting of sales data with their dates.

I have a SQL query that relies on a specific date benchmark.

This query supposedly returns only one numeric value. Now I set the condition to be dependant on the benchmark. The problem is, I don’t want this benchmark to stay constant. I need the query to return 14 values for each end of month benchmark from January 2020 to February 2021.

So the result would probably be something like this,

Which sets the benchmark for every end of the month. Note that I do not have access to CREATE anything.

I was thinking of a while loop or a temporary table, but those require me to CREATE something which always returns “Error Code: 1142. CREATE command denied to the user ….” or “Error Code: 1044. Access denied for the user ….”.

Thanks for any help or feedback.

Advertisement

Answer

You can list the dates you want and aggregate:

Note that you can also generate the dates using a recursive CTE or calendar table.

For this particular query, you could also use a cumulative sum:

That works for your particular example, but might not work for other logic.

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