Skip to content
Advertisement

SQL how to find the product group with highest porportion of expensive items

I have a table with products, product_group and price level. How can I find the product group with the highest porportion of expensive items?

I have tried this query, but it keeps all price_levels, not just the expensive ones.

Essentially, I want to divide the number of expensive items in one product group by the total number of items in the same product group.

Desired output:

Advertisement

Answer

You can use conditional aggregation:

The use of avg() is a convenient way to get the ratio you want. A more verbose method would be:

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