0

I have a table which have fields like fieldname and fieldvalue. I am trying to turn this table into another format.

For example, I have below table with fields and values:

-----------------------------
|  Fieldname   | Fieldvalue |
-----------------------------
|   Matnr      |  001       |
|   Werks      | 1000       |
|   Statu      |   01       |
-----------------------------

I want to create a internal table with the following column names and values like this:

-------------------------------
|  Matnr  |  Werks  |  Statu  |
-------------------------------
   001       1000       01

How can I do this?

Silver Paw
  • 11
  • 1
  • 2
    I see that you tagged your question [tag:opensql], how is your question related to OpenSQL? (I just see an ABAP question here) – Sandra Rossi Oct 29 '20 at 13:34
  • Is the first table an internal table or a database table? And can you guarantee that each value for `Fieldname` will occur only once and map to a column of your second structure? – Philipp Oct 29 '20 at 16:42
  • 1
    you want pivoting or transposing the itab, similar question already [were raised here](https://stackoverflow.com/questions/27163908/how-to-transpose-an-internal-table-rows-into-columns/27646386). This is natively not supported but there are multiple custom snippets that do this, e.g. [this one](http://zevolving.com/2013/11/abap-transpose-internal-table/) – Suncatcher Oct 30 '20 at 11:42

1 Answers1

0

You can use conditional aggregation:

select max(case when fieldname = 'Matnr' then fieldvalue end) as matnr,
       max(case when fieldname = 'Werks' then fieldvalue end) as Werks,
       max(case when fieldname = 'Statu' then fieldvalue end) as Statu
from t;
   
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786