1

I am trying to pull records whose arrays only meet a certain condition.

For example, I want only the results that contain "IAB3".

Here is what the table looks like

Table Name:

bids

Column Names:

BidderBanner / WinCat

Entries:

1600402 / null
1911048 / null
1893069 / [IAB3-11, IAB3]
1214894 / IAB3

How I initially thought it would be

SELECT * FROM bids WHERE WinCat = "IAB3" 

but I get an error that says no match for operator types array, string.

The database is in Google Big Query.

Community
  • 1
  • 1
kaecvtionr
  • 155
  • 1
  • 2
  • 9

4 Answers4

2

Below is for BigQuery Standard SQL

#standardSQL
SELECT * FROM `project.dataset.bids` WHERE 'IAB3' IN UNNEST(WinCat)

You can test, play with above using sample data from your question as in example below

#standardSQL
WITH `project.dataset.bids` AS (
  SELECT 1600402 BidderBanner, NULL WinCat UNION ALL
  SELECT 1911048, NULL UNION ALL
  SELECT 1893069, ['IAB3-11', 'IAB3'] UNION ALL
  SELECT 1214894, ['IAB3'] 
)
SELECT * FROM `project.dataset.bids` WHERE 'IAB3' IN UNNEST(WinCat)  

with result

enter image description here

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
1

you need to use single quotes in sql for all strings. it should be WHERE WinCat = 'IAB3' not WHERE WinCat = "IAB3"

jtylerm
  • 482
  • 4
  • 15
  • 1
    For reference, see [What is the difference between single and double quotes in SQL?](https://stackoverflow.com/questions/1992314/what-is-the-difference-between-single-and-double-quotes-in-sql) – showdev Apr 11 '19 at 21:23
1

One method uses unnest(), something like this:

SELECT b.*
FROM bids b
WHERE 'IAB3' IN (SELECT unnest(b.WinCats))

However, array syntax varies among the databases that support them and they are no part of "standard SQL".

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

this will work:

SELECT * FROM bids WHERE REGEXP_LIKE (WinCat, '(.)*(IAB3)+()*'); 
Nikhil S
  • 3,786
  • 4
  • 18
  • 32