如何在bigquery中删除/更新嵌套数据

时间:2017-12-08 03:02:33

标签: google-bigquery dml

有没有办法删除/更新bigquery中的嵌套字段?

假设我有这个数据

wives.age   wives.name  name     
21             angel    adam     
20             kale      
21           victoria   rossi    
20           jessica         

或在json:

{"name":"adam","wives":[{"name":"angel","age":21},{"name":"kale","age":20}]}
{"name":"rossi","wives":[{"name":"victoria","age":21},{"name":"jessica","age":20}]}

从上面的数据可以看出。 亚当有两个妻子,名叫天使和羽衣甘蓝。如何:

  1. 删除羽衣甘蓝记录。
  2. 将jessica更新为dessica
  3. 我试图谷歌这个,但找不到它。我也试图取消等等,但没有运气。

    我们想要这样做的原因是因为我们将数组插入到错误的记录中,并希望在某些条件下删除/更新数组数据。

1 个答案:

答案 0 :(得分:2)

以下是BigQuery Standard SQL

  
#standardSQL
WITH updates AS (
  SELECT 'rossi' name, 'jessica' oldname, 'dessica' newname UNION ALL
  SELECT 'rossi' name, 'victoria' oldname, 'polly' newname UNION ALL
  SELECT 'adam' name, 'angel' oldname, 'jen' newname 
), divorces AS (
  SELECT 'adam' name, 'kale' wifename UNION ALL
  SELECT 'adam' name, 'milly' wifename UNION ALL
  SELECT 'rossi' name, 'linda' wifename      
)
SELECT t.name, 
  ARRAY(
    SELECT AS STRUCT 
      age, 
      CASE 
        WHEN NOT oldname IS NULL THEN newname
        ELSE name 
      END name
    FROM UNNEST(wives)
    LEFT JOIN UNNEST(updates) ON t.name = u.name AND name = oldname
    LEFT JOIN UNNEST(divorces) AS wifename ON t.name = d.name AND name = wifename
    WHERE wifename IS NULL
  ) waves
FROM `project.dataset.table` t
LEFT JOIN (
  SELECT name, ARRAY_AGG(STRUCT(oldname, newname)) updates
  FROM updates GROUP BY name
  ) u ON t.name = u.name
LEFT JOIN (
  SELECT name, ARRAY_AGG(wifename) divorces
  FROM divorces GROUP BY name
  ) d ON t.name = d.name

您可以使用以下虚拟数据进行上述测试/播放

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'adam' name, [STRUCT<age INT64, name STRING>(21, 'angel'), (20, 'kale'), (22, 'milly')] wives UNION ALL
  SELECT 'rossi', [STRUCT<age INT64, name STRING>(21, 'victoria'), (20, 'jessica'), (23, 'linda')]
), updates AS (
  SELECT 'rossi' name, 'jessica' oldname, 'dessica' newname UNION ALL
  SELECT 'rossi' name, 'victoria' oldname, 'polly' newname UNION ALL
  SELECT 'adam' name, 'angel' oldname, 'jen' newname 
), divorces AS (
  SELECT 'adam' name, 'kale' wifename UNION ALL
  SELECT 'adam' name, 'milly' wifename UNION ALL
  SELECT 'rossi' name, 'linda' wifename      
)
SELECT t.name, 
  ARRAY(
    SELECT AS STRUCT 
      age, 
      CASE 
        WHEN NOT oldname IS NULL THEN newname
        ELSE name 
      END name
    FROM UNNEST(wives)
    LEFT JOIN UNNEST(updates) ON t.name = u.name AND name = oldname
    LEFT JOIN UNNEST(divorces) AS wifename ON t.name = d.name AND name = wifename
    WHERE wifename IS NULL
  ) waves
FROM `project.dataset.table` t
LEFT JOIN (
  SELECT name, ARRAY_AGG(STRUCT(oldname, newname)) updates
  FROM updates GROUP BY name
  ) u ON t.name = u.name
LEFT JOIN (
  SELECT name, ARRAY_AGG(wifename) divorces
  FROM divorces GROUP BY name
  ) d ON t.name = d.name

结果符合预期

name    waves.age   waves.name   
adam    21          jen  
rossi   21          polly    
        20          dessica  

我希望您能够将上述内容应用于您的真实案例:o)