Skip to content
Advertisement

Oracle create a Pivot table from 3 columns

I’m trying to create an excel-like pivot table with 3 columns of information, order tpye, product code, and the income generated from that order.

I’ve managed to get the generated income for every order from the same product, ordered by the order tpye.

A fragment from the output query:

As you can see I have now a couple of rows for every order type because of the differenct products.

How can I modify this query to get a pivot table which has the columns for every order type and the rows for the product codes, so I only have one column for the order types instead of rows?

For example:

Advertisement

Answer

You tagged Oracle18c so this should work for your version. I tested this on 11g.

This does require the set for the IN list to be filled in prior to execution. There is another syntax that allows a sub select but it returns XML. If you try to replace PIVOT XML with PIVOT it gives an error.

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