Skip to content
Advertisement

How to get maximum of Five consecutive values in an Year?

I have a yearly list of sales of an item for the past 20 years.

the data is like this.

I have a behavioral problem. I must find the five consecutive days where the sum of sales is maximum in a year, for each year, for the past 20 years. Then using the result i must analyse the spending pattern.

how can i get the 5 consecutive days whose sum is maximum in a year?

I must get it for all years with dates and sum of sales in those 5 days total value. Can anyone help me in my assignment, please?

TIA

Advertisement

Answer

Well, in MySQL 8+, you can use window functions. In earlier versions, a correlated subquery. That looks like:

Notes:

  • You will need to reset the group_concat_max_len, because 1024 is probably not long enough for the intermediate result.
  • This allows the periods to span year boundaries.

In MySQL 8, use window functions!

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