Skip to content
Advertisement

Convert columns to rows in Firebird (unpivot command)

Which is the similar command for unpivot in Firebird? TKS in advance…

My Table

 Id  |  Name    |  Land  |   Water
-----|----------|--------|---------
 1   |  John    |  300m  |    100m
-----|----------|--------|---------
 2   |  Mark    |  100m  |     0m
-----------------------------------

Desired Result

 Id   |  Name  |   Category | Surface
 -----|--------|------------|--------
 1    |  John  |    Land    |   300m
 -----|--------|------------|--------
 1    |  John  |    Water   |   100m
 -----|--------|------------|--------
 2    |  Mark  |    Land    |   100m
 -----|--------|------------|--------
 2    |  Mark  |    Water   |    0m

Advertisement

Answer

You can use union all:

select id, col1 as col
from t
union all
select id, col2 as col
from t;

Something like this should work for most purposes.

EDIT:

For your particular data:

select id, name, 'Land' as category, land as surface
from mytable
union all
select id, name, 'Water' as category, water as surface
from mytable;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement