进行“移调".使用SQL的表 [英] Taking the "transpose" of a table using SQL

查看:78
本文介绍了进行“移调".使用SQL的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道此操作是否有名称,但它与线性代数中的转置类似.

I don't know if there is a name for this operation but it's similar to the transpose in linear algebra.

是否有一种方法可以将n表T1变为1,例如

Is there a way to turn an 1 by n table T1 such as

c_1|c_2|c_3|...|a_n
-------------------
1  |2  |3  |...|n

插入n x 2表,如下所示

Into a n by 2 table like the following

key|val
-------
c_1|1
b_2|2
c_3|3
.  |.
.  |.
a_n|n

我假设T1中的每一列c_i不太可能被识别.

I am assuming that each column c_i in T1 can be unlikely identified.

推荐答案

基本上,您需要UNPIVOT该数据,可以使用UNION ALL进行此操作:

Basically, you need to UNPIVOT this data, you can perform this using a UNION ALL:

select 'c_1' col, c_1 value
from yourtable
union all
select 'c_2' col, c_2 value
from yourtable
union all
select 'c_3' col, c_3 value
from yourtable

这篇关于进行“移调".使用SQL的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆