Ravi Kishore
Ravi Kishore

Reputation: 1

How to Delete rows from Structure in bigquery

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

Upvotes: 0

Views: 917

Answers (2)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172974

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

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269543

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;

Upvotes: 0

Related Questions