问题
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