简体   繁体   English

将列取消透视成行(oracle)

[英]Unpivot columns into rows (oracle)

much like this original SO Pivoting rows into columns dynamically in Oracle 类似于此原始SO,因此在Oracle中动态将行旋转到列中

but i would like to do the opposite 但我想相反

how can i get 我怎样才能得到

 ID       NAME    AGE    GENDER  STATUS
----     -----   -----  ------  --------
  1      Bob      30     male 
  2      Susan                   married

into this 进入这个

ID       K       V
----     -----   -----
  1      name    Bob
  1      age     30
  1      gender  male
  2      name    Susan
  2      status  married

You are looking for unpivot . 您正在寻找unpivot

select * from t 
unpivot (
          v for k in ("NAME","AGE","GENDER","STATUS")
        ) u

You may have a type mismatch if the age column is an integer. 如果age列是整数,则类型可能不匹配。 In that case convert it to a character before unpivoting. 在这种情况下,请先将其转换为字符,然后再进行旋转。

select *
from (select id,name,to_char(age) age,gender,status from t) t 
unpivot (
         v for k in ("NAME","AGE","GENDER","STATUS")
        ) u

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM