Welcome Guest! Log in
Stambia versions 2.x, 3.x, S17, S18, S19 and S20 are reaching End of Support January, 15th, 2024. Please consider upgrading to the supported Semarchy xDI versions. See Global Policy Support and the Semarchy Documentation.

The Stambia User Community is moving to Semarchy! All the applicable resources have already been moved or are currently being moved to their new location. Read more…


This article is now available on our new support portal: 
https://support.semarchy.com/en/support/solutions/articles/43000658750-how-to-pivot-or-unpivot-a-table

Using Query

If you are working on a database that is capable of pivoting or unpivoting tables, you can simply write a query that makes the transformation and reverse it in Stambia.

Here is an example of how to do it using MSSQL Pivot operator.

Write a query that gives you the desired transformed table in MS SQL:

 MS SQL Query

In the MS SQL metadata create a new query folder and a new query. Give it a name and paste the query:

 Query in metadata

Save the metadata and the right click on query and choose Reverse from Actions:

 Reverse query

Save the metadata again and now you can use the result of the query as a source in your mappings:

Reversed query in a mapping

Using Stages

In all other cases it is possible to make this kind of transformation in Stambia using stages.

Note if you do not have a database that you can use to create a stage you can use the internal H2 database included in the runtime (How to use internal H2 database?)

Example of pivot:

In this example we have a source table with a single line per quarter and we want to transform that to have a single line with 4 columns containing values per quarter.

 Source unpivoted table

To do that we will need to add to our mapping four stages. Each Stage will be alimented with values from one quarter only (you need to add appropriate filter on each:

 Pivot mapping

The stages should be then joined so they can be used to feed the columns of the target table:

 Pivoted target table

Example of unpivot:


We can also imagine a situation where in the source of our mapping we have a table with quarterly values in columns and we want to feed a target table where each quarterly value will be represented in one line.

In this case we will need one stage with four sets:

 Unpivot mapping

Each set represents a quarter with QNO value hardcoded and corresponding value mapped from source table. All sets are combined with UNION operator and can be used to feed the target “unpivoted” table.

Articles

Suggest a new Article!