Yousif Hassan
Yousif Hassan

Reputation: 23

How to turn a matrix into a usable table using SQL

I'm working on converting a matrix into a usable table but couldn't come up with a creative way by using SQL and basically restructuring the entire file

Any help/recommendations would be greatly appreciated.

BEFORE

     A       B       C       D
1   .5      .6      .7      .8      
2   .9      1.0     1.1     1.2
3   1.3     1.4     1.5     1.6
4   1.7     1.8     1.9     2.0

AFTER

X       Y       Result
A       1       .5
A       2       .9
A       3       1.3
A       4       1.7
.....

Upvotes: 0

Views: 121

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270411

In MySQL, the simplest method is probably union all:

select 'A' as x, col1 as y, A as result from t union all
select 'B' as x, col1 as y, B as result from t union all
select 'C' as x, col1 as y, C as result from t union all
select 'D' as x, col1 as y, D as result from t;

Upvotes: 1

Related Questions