BigQuery SPLIT手动创建表

时间:2017-07-13 09:36:46

标签: sql r google-bigquery

参考此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。不幸的是我无法转换我的代码。我只能使用Legacy SQL。

编辑: 预期表应如下所示:

-- +---------+--------+
-- | Title   | Name   |
-- +---------+--------+
-- | Title 1 | Name 1 |
-- | Title 2 | Name 2 | 
-- | Title 3 | Name 3 |
-- | Title 4 | Name 4 |
-- +---------+--------+

1 个答案:

答案 0 :(得分:1)

以下是BigQuery Standard 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