presto 将单列值拆分为多行
presto split single column value to multiple rows
我在 presto 上有一个 table 有多个记录的记录。从那条记录中,我使用了这个简单的 SQL 查询,
select id, data from my_table where id IN (1,7)
这是我从那个查询中得到的,
id data
1 ('A', 0.0, 12)
7 ('B', 0.0, 20) ('A', 0.0, 30) ('C', 0.0, 40)
现在我想让这个数据列像下面这样多行,基本上,将单列值拆分成多行。
name value age
A 0.0 12
B 0.0 20
A 0.0 30
C 0.0 40
到目前为止我已经尝试过,但是在 presto 上遇到错误
SELECT data
FROM my_table
CROSS APPLY STRING_SPLIT(data, ' ') where id IN (1,7);
我的想法是 用 space 拆分列值,然后用逗号再次拆分以在末尾生成多个列。似乎我需要利用 here 中的 split()
和 split_part()
,但我无法使其工作。请告诉我该怎么做?
使用 split
、unnest
、replace
和 try
我现在可以进行查询了 只是想知道有没有其他方式 do/fix 吗? 因为我对这种方式不完全满意:)
WITH t AS (
select data from my_table where id IN (1,7)
)
SELECT
try(trim(data_parts[1])) AS name,
try(trim(data_parts[2])) AS age,
try(trim(data_parts[3])) AS value
FROM (
SELECT split(replace(split_Col2,'('),',') as data_parts
FROM t
CROSS JOIN UNNEST(SPLIT(data,')')) AS t (split_Col2)
) WHERE length(try(trim(data_parts[1]))) > 0
结果
name age value
34 'AAAA' 0.0 'NNNNN'
36 'BBBB' 0.0 'NNNNN'
38 'CCCC' 0.0 'MMMMM'
39 'AAAA' 0.0 'CCCCC'
我在 presto 上有一个 table 有多个记录的记录。从那条记录中,我使用了这个简单的 SQL 查询,
select id, data from my_table where id IN (1,7)
这是我从那个查询中得到的,
id data
1 ('A', 0.0, 12)
7 ('B', 0.0, 20) ('A', 0.0, 30) ('C', 0.0, 40)
现在我想让这个数据列像下面这样多行,基本上,将单列值拆分成多行。
name value age
A 0.0 12
B 0.0 20
A 0.0 30
C 0.0 40
到目前为止我已经尝试过,但是在 presto 上遇到错误
SELECT data
FROM my_table
CROSS APPLY STRING_SPLIT(data, ' ') where id IN (1,7);
我的想法是 用 space 拆分列值,然后用逗号再次拆分以在末尾生成多个列。似乎我需要利用 here 中的 split()
和 split_part()
,但我无法使其工作。请告诉我该怎么做?
使用 split
、unnest
、replace
和 try
我现在可以进行查询了 只是想知道有没有其他方式 do/fix 吗? 因为我对这种方式不完全满意:)
WITH t AS (
select data from my_table where id IN (1,7)
)
SELECT
try(trim(data_parts[1])) AS name,
try(trim(data_parts[2])) AS age,
try(trim(data_parts[3])) AS value
FROM (
SELECT split(replace(split_Col2,'('),',') as data_parts
FROM t
CROSS JOIN UNNEST(SPLIT(data,')')) AS t (split_Col2)
) WHERE length(try(trim(data_parts[1]))) > 0
结果
name age value
34 'AAAA' 0.0 'NNNNN'
36 'BBBB' 0.0 'NNNNN'
38 'CCCC' 0.0 'MMMMM'
39 'AAAA' 0.0 'CCCCC'