You are on page 1of 7

Transposing Rows to Columns using Calculation view | SAP Blogs https://blogs.sap.com/2014/09/10/transposing-rows-to-columns-using-cal...

Home / Community / Blogs

Transposing Rows to Columns using


Calculation view
September 10, 2014 | 3,287 Views |

Monalisa Biswal
more by this author

SAP HANA

Database

share
0 share
0 tweet share
0

Follow

Share & Follow


Privacy Terms of Use Legal Disclosure Copyright Trademark Sitemap Newsletter

1 of 7 04/02/2018, 8:51 PM
Transposing Rows to Columns using Calculation view | SAP Blogs https://blogs.sap.com/2014/09/10/transposing-rows-to-columns-using-cal...

There are many nice articles available on hana modeling which talk about

transposing data from columns to rows.

In this article we will see how to transpose data from rows to columns.

Before going into the technical details, let’s take a case study.

In a table we have a product list with their selling quantities . Depending on


the selling quantities, products need to be categorized and an aggregated
report needs to be generated to show count of products falling in a specific
category.

e.g.

If the Quantity > =500, Category = “HIGH”

If the Quantity >=100 and < 500, Category = “MEDIUM”

If the Quantity < 100, Category = “LOW”

We need a report in following format

2 2 1

1 3 1

Create an analytic view and map all fields from the database table in
the output.

2 of 7 04/02/2018, 8:51 PM
Transposing Rows to Columns using Calculation view | SAP Blogs https://blogs.sap.com/2014/09/10/transposing-rows-to-columns-using-cal...

4 Comments
You must be Logged on to comment or reply to a post.

3 of 7 04/02/2018, 8:51 PM
Transposing Rows to Columns using Calculation view | SAP Blogs https://blogs.sap.com/2014/09/10/transposing-rows-to-columns-using-cal...

After activating the analytic view we can see its data in data preview
as below:

Create a SQL Script type calculation view

4 of 7 04/02/2018, 8:51 PM
Transposing Rows to Columns using Calculation view | SAP Blogs https://blogs.sap.com/2014/09/10/transposing-rows-to-columns-using-cal...

In the calculation view, Add the target columns as required in the


result of transpose(Location, Quantity for High , Quantity for Medium,
Quantity for Low).

Now in the script we will need to write queries to populate the target
resultset.
For each of the new column in the resultset we will write a separate
query to fetch corresponsing information.
Since the analytic view doesn’t have these three columns we will use

5 of 7 04/02/2018, 8:51 PM
Transposing Rows to Columns using Calculation view | SAP Blogs https://blogs.sap.com/2014/09/10/transposing-rows-to-columns-using-cal...

constants to populate the new columns added in the resultset.

e.g. to add a column (ColumnA) in the resultset syntax to be used

select column1, column2, <constant value> as ColumnA from


<table/view>;

For each category(HIGH/MEDIUM/LOW) we will write a query to


populate constant value ‘1’ for its respective column and ‘0’ for other
columns.
Union all result sets and finally aggregate the result set to get the
aggregated output.

Please see below the outputs of the queries and how they will merge
into final output.

6 of 7 04/02/2018, 8:51 PM
Transposing Rows to Columns using Calculation view | SAP Blogs https://blogs.sap.com/2014/09/10/transposing-rows-to-columns-using-cal...

After activating the calculation view, you will be able to see data in the
transposed format.

Alert Moderator

7 of 7 04/02/2018, 8:51 PM

You might also like