For example, one column in my table is an array, I want to check if that column contains an element that contains substring "denied" (so elements like "denied at 12:00 pm", "denied by admin" will all count, I believe I will have to use "like" to identify the pattern). How to write sql for this?
Asked
Active
Viewed 7.9k times
21
-
Please add sample data and expected outcome – mtr.web Jun 08 '18 at 20:04
4 Answers
32
Use presto's array functions:
filter()
, which returns elements that satisfy the given conditioncardinality()
, which returns the size of an array:
Like this:
where cardinality(filter(myArray, x -> x like '%denied%')) > 0

Bohemian
- 412,405
- 93
- 575
- 722
-
8You could `reduce(myArray, false, (a, x -> a OR x like '%denied%'))` to avoid copying an array for every row. – Piotr Findeisen Jun 09 '18 at 09:03
14
In newer versions of PrestoSQL (now known as Trino), you can use the any_match
function:
WHERE any_match(column, e -> e like '%denied%')

user5305519
- 3,008
- 4
- 26
- 44

Martin Traverso
- 4,731
- 15
- 24
2
-
2this does not answer the question. According to the doc, the `CONTAINS` operator only looks for exact matches. – bmarcov Apr 29 '20 at 10:58
0
We can use strpos(returns starting position of substring and 0 if not found) here. (documentation)
where strpos(array_column,'denied')>0

AGVG
- 36
- 4