Situation:
I have a database that contains sales records, each record has a ID (PK), ProductCode, Year, Month, SalesVolume.
As a user I will specify the year, so if I specify 1980, I will query the database for all the records that correspond to that year.
I am trying to create a query that will total the months SalesVolumes for the specified year, then pick the top 5 sales volume values.
What I have gathered so far is to somehow do the above, then to choose the top 5 sales volumes values, put them in descending order, and select the top 5, but that’s as far as I have got
Note in a year there will be SalesVolumes for multiple products:
ID, Product Code, Year, Month, SalesVolume
23041 121 1980 1 21
23042 121 1980 2 960
23043 121 1980 3 939
23044 121 1980 4 927
23045 121 1980 5 931
23046 121 1980 6 950
23047 121 1980 7 975
23048 121 1980 8 994
23049 121 1980 9 994
23050 121 1980 10 968
23051 121 1980 11 918
23052 121 1980 12 854
23425 122 1980 1 1002
23426 122 1980 2 1032
23427 122 1980 3 1090
23428 122 1980 4 1062
23429 122 1980 5 1010
23430 122 1980 6 1103
23431 122 1980 7 1214
23432 122 1980 8 1122
23433 122 1980 9 1019
23434 122 1980 10 1181
23435 122 1980 11 1343
23436 122 1980 12 1180
Expected Output:
For 1980
Product Code, SalesVolume
121 , (total SalesVolume of the 12 months)
122 , (total SalesVolume of the 12 months)
Here’s what you can do – total up the sales for each item by for a given year, rank them, and then select the top 5 ranking items.
If you want to do select the top 5 per year for all years, you can do that too: