How to Delete rows from Structure in bigquery

≡放荡痞女 提交于 2021-02-11 12:28:50

问题


Can you please help me with my question, i am new to Bigquery.

I have a table with multiple "record" type fields. I need to delete a row from one of the record. Consider below example as:

id         date     subid.id    subid.flag
1234    1/4/2020      1234-1        1
                      1234-2        1
                      1234-3        1
                      1234-4       -1
5678    1/5/2020      5678-1        1
                      5678-2        1

My requirement from the above is to delete the row from the structure subid with flag -1. What is the best way to do this ? Please help.

sample data


回答1:


Below is for BigQuery Standard SQL

#standardSQL
SELECT * EXCEPT(subid),
  ARRAY(
    SELECT AS VALUE subid 
    FROM t.subid  WITH OFFSET
    WHERE flag != -1 
    ORDER BY OFFSET 
  ) AS subid
FROM `project.dataset.table` t



回答2:


You can unnest and reaggregate. If I understand correctly:

select id, date,
       (select array_agg(subid order by n)
        from unnest(t.subid) subid with offset as n
        where subid.flag <> -1
       ) as subid
from t;


来源:https://stackoverflow.com/questions/61521999/how-to-delete-rows-from-structure-in-bigquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!