我想知道如何在Google Big查询中执行CONNECT BY REGEXP

时间:2019-01-25 05:37:06

标签: regex google-bigquery oracle-sqldeveloper connect-by

我在oracle sql中有以下语句,我想在Google Big Query中运行它。

CONNECT BY REGEXP_SUBSTR(VALUE, '[^,]+', 1, LEVEL) IS NOT NULL)

如何在Big查询中运行以上代码?

1 个答案:

答案 0 :(得分:1)

我在这里猜测-但通常此结构用于所谓的字符串分解

因此,在BigQuery中,您可以使用SPLIT(value)或REGEXP_EXTRACT_ALL(value,r'[^,] +'),如以下示例所示

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, '1,2,3,4,5,6,7' AS value UNION ALL
  SELECT 2, 'a,b,c,d'
)
SELECT id, SPLIT(value) value
FROM `project.dataset.table`   

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, '1,2,3,4,5,6,7' AS value UNION ALL
  SELECT 2, 'a,b,c,d'
)
SELECT id, REGEXP_EXTRACT_ALL(value, r'[^,]+') value
FROM `project.dataset.table`  

以上查询都将返回

Row id  value    
1   1   1    
        2    
        3    
        4    
        5    
        6    
        7    
2   2   a    
        b    
        c    
        d     

在这里,如您所见-每行中的值都被拆分为元素数组,但仍在同一行中

要展平结果,您可以进一步使用UNNEST(),如以下示例所示

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, '1,2,3,4,5,6,7' AS value UNION ALL
  SELECT 2, 'a,b,c,d'
)
SELECT id, value
FROM `project.dataset.table`,
UNNEST(SPLIT(value)) value  

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, '1,2,3,4,5,6,7' AS value UNION ALL
  SELECT 2, 'a,b,c,d'
)
SELECT id, value
FROM `project.dataset.table`, 
UNNEST(REGEXP_EXTRACT_ALL(value, r'[^,]+')) value   

都在结果下方返回(所有提取的元素在单独的行中)

Row id  value    
1   1   1    
2   1   2    
3   1   3    
4   1   4    
5   1   5    
6   1   6    
7   1   7    
8   2   a    
9   2   b    
10  2   c    
11  2   d