简体   繁体   中英

Bigquery Select from table where any column contains 'FINISHED'

I have table in bigquery which has over 100 columns and has more than 70 Million row. I want to find out if I can write a query to extract rows where any of the column contains a value 'FINISHED'.

Below is for BigQuery Standard SQL Should be good start for you :)

#standardSQL
SELECT <columns to output> 
FROM yourTable AS t
WHERE REGEXP_CONTAINS(LOWER(TO_JSON_STRING(t)), 'finished')

You can test/play with it with below dummy data

#standardSQL
WITH yourTable AS (
  SELECT 'a' AS x, 'b' AS y, 'c' AS z UNION ALL
  SELECT 'finished', '', '' UNION ALL
  SELECT '', 'Bigquery Select from table where any column contains "FINISHED"','' UNION ALL
  SELECT '', '', 'aaa' UNION ALL
  SELECT 'finished', 'bbb', 'finished' 
)
SELECT * 
FROM yourTable AS t
WHERE REGEXP_CONTAINS(LOWER(TO_JSON_STRING(t)), 'finished')   

Update

Note: if you have your search word as a part of at least one column name - above will return all rows! To address this - you would need to invest a little bit more coding

For example, for simple schema (with no record or repeated) this would be a

#standardSQL
SELECT <columns to output>  
FROM yourTable AS t
WHERE (SELECT COUNTIF(SPLIT(zzz, ':')[SAFE_OFFSET(1)] LIKE '%finished%') 
  FROM UNNEST(SPLIT(SUBSTR(LOWER(TO_JSON_STRING(t)),2,LENGTH(TO_JSON_STRING(t))-2))) AS zzz
  ) > 0   

You can test this with below

#standardSQL
WITH yourTable AS (
  SELECT 'a' AS x, 'b' AS y, 'c' AS col_finished UNION ALL
  SELECT 'finished', '', '' UNION ALL
  SELECT '', 'Bigquery Select from table where any column contains "FINISHED"','' UNION ALL
  SELECT '', '', 'aaa' UNION ALL
  SELECT 'finished', 'bbb', 'finished' 
)
SELECT * 
FROM yourTable AS t
WHERE (SELECT COUNTIF(SPLIT(zzz, ':')[SAFE_OFFSET(1)] LIKE '%finished%') 
  FROM UNNEST(SPLIT(SUBSTR(LOWER(TO_JSON_STRING(t)),2,LENGTH(TO_JSON_STRING(t))-2))) AS zzz
  ) > 0  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM