Skip to content
Advertisement

PL SQL Pivot: use different aggregates

I want to pivot sum(sales) per country as below. But the price column should be an average per shop. So, I would love to have this result:

Desired Result:

shop avg_price US CN
2 6 100 10
3 5 60 10

Current Result:

shop price US CN
2 5 null 5
2 7 100 5
3 5 60 10

I could skip price within the pivot part and simply rejoin it later on. However, I believe there is a smarter option. Can you help me out?

Query (so far)

Advertisement

Answer

You need to group by shop while adding double-quoted aliases for each pivoted columns such as

Demo

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