0

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

Community
  • 1
  • 1
  • Does this answer your question? [Google BigQuery Delete Rows?](https://stackoverflow.com/questions/10604135/google-bigquery-delete-rows) – Digvijay S Apr 30 '20 at 11:39
  • 1
    Hi Digvijay, Thanks for the reply. I don't want to delete the entire row but i want to delete a certain rows from the structure and keep the remaining data from that row intact. The answer given by @Gordon Linoff is useful in my case. – Ravi Kishore Apr 30 '20 at 11:51

2 Answers2

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
Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
  • @Mikhal - How to delete this row? This command is not a delete statement. Thanks – Nguai al Mar 04 '23 at 22:28
  • i just double checked and confirming - above query returns result with row (that has flag -1) being removed! if you want to overwrite original table - you should use create or replace syntax followed by that query! – Mikhail Berlyant Mar 04 '23 at 22:56
0

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;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786