简体   繁体   English

AWS ATHENA 将列转置为多行

[英]AWS ATHENA Transpose from column into multiple rows

I have a csv file uploaded to an S3 bucket which I pick up with AWS Glue then query using Athena.我有一个 csv 文件上传到一个 S3 存储桶,我使用 AWS Glue 获取该存储桶,然后使用 Athena 进行查询。 The CSV table is in the format below: CSV 表格式如下:

ID ID Starting Time起始时间 Ending Time结束时间 Failure Sector故障部门 Recycling Rounds回收轮
1 1 5am/8am/1pm早上 5 点/早上 8 点/下午 1 点 6am/10am/3pm早上 6 点/上午 10 点/下午 3 点 /(5)/ /(5)/ 0/0/ 0/0/
2 2 4am/6am/3pm/7pm凌晨 4 点/早上 6 点/下午 3 点/晚上 7 点 7am/8am/5pm/11pm上午 7 点/上午 8 点/下午 5 点/晚上 11 点 ///(1) ///(1) 1//1/ 1//1/

I want to convert that format into this:我想将该格式转换为:

ID ID Starting Time起始时间 Ending Time结束时间 Failure Sector故障部门 Recycling Rounds回收轮
1 1 5am上午05时 6am早上 6 点 blank空白的 0 0
1 1 8am早上 8 点 10am上午 10 点 (5) (5) 0 0
1 1 1pm下午 1 点 3pm下午 3 点 blank空白的 blank空白的
2 2 4am凌晨 4 点 7am早上7点 blank空白的 1 1
2 2 6am早上 6 点 8am早上 8 点 blank空白的 blank空白的
2 2 3pm下午 3 点 5pm下午 5 点 blank空白的 1 1
2 2 7pm晚上 7 点 11pm晚上 11 点 (1) (1) blank空白的

How do I accomplish that using SQL in Athena?如何在 Athena 中使用 SQL 来实现这一点? Any help is appreciated.任何帮助表示赞赏。 Thanks!谢谢!

You can use split to transform varchar into array of varchars and then flatten with unnest which supports multiple arrays:您可以使用split将 varchar 转换为 varchars 数组,然后使用支持多个 arrays 的unnest进行展平:

-- sample data
with dataset (ID, Starting_Time, Ending_Time, Failure_Sector, Recycling_Rounds) AS
         (VALUES (1, '5am/8am/1pm', '6am/10am/3pm', '/(5)/', '0/0/'),
                 (2, '4am/6am/3pm/7pm', '7am/8am/5pm/11pm', '///(1)', '1//1/'))

-- query
select st, et, fs, rr
from dataset,
unnest (split(Starting_Time,'/'), split(Ending_Time,'/'), split(Failure_Sector,'/'), split(Recycling_Rounds,'/')) 
    as t(st, et, fs, rr);

Output: Output:

st英石 et fs fs rr rr
5am上午05时 6am早上 6 点 0 0
8am早上 8 点 10am上午 10 点 (5) (5) 0 0
1pm下午 1 点 3pm下午 3 点
4am凌晨 4 点 7am早上7点 1 1
6am早上 6 点 8am早上 8 点
3pm下午 3 点 5pm下午 5 点 1 1
7pm晚上 7 点 11pm晚上 11 点 (1) (1)

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

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