Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"contains" in Bigquery standard SQL

I wish to migrate from Legacy SQL to Standard SQL

I had the following code in Legacy SQL

SELECT
  hits.page.pageTitle
FROM [mytable] 
WHERE hits.page.pageTitle contains '%' 

And I tried this in Standard SQL:

SELECT
  hits.page.pageTitle
FROM `mytable` 
WHERE STRPOS(hits.page.pageTitle, "%") 

But it gives me this error:

Error: Cannot access field page on a value with type ARRAY> at [4:21]

like image 964
MFR Avatar asked Jun 15 '17 04:06

MFR


People also ask

What is standard SQL in BigQuery?

Previously, BigQuery executed queries using a non-standard SQL dialect known as BigQuery SQL. With the launch of BigQuery 2.0, BigQuery released support for standard SQL, and renamed BigQuery SQL to legacy SQL. Standard SQL is the preferred SQL dialect for querying data stored in BigQuery.

How can I remove special characters from a string in BigQuery?

BigQuery RegExp: How to replace special characters To replace special characters, you can use regular expressions like this [^a-zA-Z0-9]+ and REGEXP_REPLACE function.

WHAT IS WITH clause in BigQuery?

The WITH clause contains one or more common table expressions (CTEs). Each CTE binds the results of a subquery to a table name, which can be used elsewhere in the same query expression. BigQuery does not materialize the results of non-recursive CTEs within the WITH clause.

How do I split text in BigQuery?

Using SPLIT(value[, delimiter]) returns an array. Then using SAFE_OFFSET(zero_based_offset) or SAFE_ORDINAL(one_based_offset) to get item from array. SELECT SPLIT(app_info.


1 Answers

Try this one:

SELECT 
  hits.page.pageTitle
FROM `table`,
UNNEST(hits) hits
WHERE REGEXP_CONTAINS(hits.page.pageTitle, r'%')
LIMIT 1000

In ga_sessions schema, "hits" is an ARRAY (that is, REPEATED mode). You need to apply the UNNEST operation in order to work with arrays in BigQuery.

like image 149
Willian Fuks Avatar answered Oct 08 '22 19:10

Willian Fuks