BigQuery SPLIT 手动创建表
Posted
技术标签:
【中文标题】BigQuery SPLIT 手动创建表【英文标题】:BigQuery SPLIT manual creation of table 【发布时间】:2017-07-13 09:36:45 【问题描述】:参考这个question,我想手动创建多个列。
SELECT SPLIT(Titles) AS Title
FROM (SELECT 'Title 1,Title 2,Title 3,Title 4' AS Titles)
我尝试过像这样简单地添加新列:
SELECT SPLIT(Titles) AS Title, SPLIT(Names) AS Name,FROM (SELECT 'Title 1,Title 2,Title 3,Title 4' AS Titles, 'Name 1,Name 2,Name 3,Name 4' AS NAMES)
但是 BQ 向我显示以下错误:
Error: Cannot output multiple independently repeated fields at the same time. Found Title and Name
我认为这可能与 BQ 如何使结果变平有关,我发现了类似的问题 here。不幸的是,我无法转换我的代码。我只能使用旧版 SQL。
编辑: 预期的表格应如下所示:
-- +---------+--------+
-- | Title | Name |
-- +---------+--------+
-- | Title 1 | Name 1 |
-- | Title 2 | Name 2 |
-- | Title 3 | Name 3 |
-- | Title 4 | Name 4 |
-- +---------+--------+
【问题讨论】:
您能否提供您期望的确切结果,因为它可能会有所不同 我编辑了这篇文章。基本上它应该添加具有相同行数的列。 【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
WITH data AS (
SELECT 'Title 1,Title 2,Title 3,Title 4' AS Titles, 'Name 1,Name 2,Name 3,Name 4' AS Names
)
SELECT
Title,
Name
FROM data,
UNNEST(SPLIT(Titles)) AS Title WITH OFFSET AS pos1,
UNNEST(SPLIT(Names)) AS Name WITH OFFSET AS pos2
WHERE pos1 = pos2
ORDER BY Title
BigQuery Legacy SQL 中的相同想法看起来更加繁琐
#legacySQL
SELECT
Title, Name
FROM FLATTEN((
SELECT Title, POSITION(Title) AS pos1
FROM (
SELECT SPLIT(Titles) AS Title
FROM (SELECT 'Title 1,Title 2,Title 3,Title 4' AS Titles, 'Name 1,Name 2,Name 3,Name 4' AS Names)
)
), pos1) AS titles
JOIN FLATTEN((
SELECT Name, POSITION(Name) AS pos2
FROM (
SELECT SPLIT(Names) AS Name
FROM (SELECT 'Title 1,Title 2,Title 3,Title 4' AS Titles, 'Name 1,Name 2,Name 3,Name 4' AS Names)
)
), pos2) AS names
ON pos1 = pos2
【讨论】:
@johnsnow - 你有机会尝试吗?以上是关于BigQuery SPLIT 手动创建表的主要内容,如果未能解决你的问题,请参考以下文章