Skip to content

Finding top 10 products sold in a year

I have these tables below along with the definition. I want to find top 10 products sold in a year after finding counts and without using aggregation and in an optimized way. I want to know if aggregation is still needed or I can accomplish it without using aggregation. Below is the query. Can anyone suggest a better approach.

Sample Data:

Tried this:

I am seeing this error:

Advertisement

Answer

I don’t understand why you don’t want to use an aggregate function when you have to aggregate over your data. This query works fine, without any issues on the GROUP BY:

This works because the productid is the primary key that has a functional dependency to the product name.

By the way, tested on version 12, but it should work on older and newer versions as well.

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