0

Is there a quick way to transfom a Table like this:

FIELD   |  VALUE
+-------+----------+
Address |  here
Name    |  Rainer
Tel     |  01234567
Other   |  idk

Into this:

Address | Name     | Tel     | Other
+-------+----------+---------+----------+
here    |  Rainer  | 01234567| idk
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
DropMania
  • 103
  • 9

2 Answers2

2

You can also use pivot method :

select * 
  from tab
 pivot(
       max(value) for field in ( 'Address' as "Address", 
                                 'Name'    as "Name", 
                                 'Tel'     as "Tel", 
                                 'Other'   as "Other" ) ) 

Demo

Barbaros Özhan
  • 47,993
  • 9
  • 26
  • 51
1

Use conditional aggregation:

select
    max(case when field = 'Address' then value end) Address,
    max(case when field = 'Name' then value end) Name
    max(case when field = 'Tel' then value end) Tel
    max(case when field = 'Other' then value end) Other
from mytable

In normal situation, you would need a column to group by, like for example a user id, so you can generate several records from the initial content.

GMB
  • 195,563
  • 23
  • 62
  • 110