简体   繁体   English

如何在 AWS Athena 或 python 中将列转换为行?

[英]How to convert columns to rows in AWS Athena or python?

Please consider this table:请考虑这张表:

ID ID Name姓名 Sector部门
1/2/5/3/4 1/2/5/3/4 NA/SA/TE/CV/BA北美/南非/TE/CV/BA 0/0/1953525168/0/0 0/0/1953525168/0/0

I want to convert this table to this one:我想把这张表转换成这张:

ID ID Name姓名 Sector部门
1 1 NA不适用 0 0
2 2 SA南非 0 0
5 5 TE TE 1953525168 1953525168
3 3 CV简历 0 0
4 4 BA文学学士 0 0

... ...

How I can do this in appropriate way?我怎样才能以适当的方式做到这一点?

You can use split 's into unnest :您可以使用split 's into unnest

-- sample data
WITH dataset (ID, Name, Sector) AS (
    VALUES ('1/2/5/3/4', 'NA/SA/TE/CV/BA', '0/0/1953525168/0/0')
)

-- query
select t.*
from dataset
,unnest (split(ID, '/'), split(Name, '/'), split(Sector, '/')) as t(ID, Name, Sector);

Output: Output:

ID ID Name姓名 Sector部门
1 1 NA不适用 0 0
2 2 SA南非 0 0
5 5 TE TE 1953525168 1953525168
3 3 CV简历 0 0
4 4 BA文学学士 0 0

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

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