Skip to content
Advertisement

Pivot One Column and Retain SUM,AVG,COUNT Columns

Let’s say I have a table PRODUCT like this.

Then I get their sum, average and count using group by.

I have this result.

I want to pivot the productname column into something like this.

I have used pivot to each aggregate then union all, which I think is too verbose and not efficient? So I’m thinking if there are other solutions for this.

Advertisement

Answer

You can achieve this by using both UNPIVOT and PIVOT.

Please see the demo here.

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