Impala Query: find value in pipe-separated list

I have a column containing strings of pipe-delimited STRING values:

|   colA    |
 ___________
| 5|4|2|255 |
| 5|4|4|0   |
| 5|4|4|3   |
| 5|4|4|4   |

      

I need to create a query that will select all rows containing 4 or 5, but never 2 or 3. Something along the lines:

SELECT t.colA
FROM my_table t
WHERE (t IN ("4", "5") AND t NOT IN ("2","3")

      

Result:

|   colA    |
 ___________
| 5|4|4|0   |
| 5|4|4|4   |

      

I ended up using a combination of the two answers below as using one of the methods still left me with lines containing only "255". Here's the final request:

SELECT t.colA
FROM my_table t
WHERE (t.colA IN ('4', '5') OR t.colA LIKE "%|5|%" 
       OR t.colA LIKE "%|5" OR t.colA LIKE "5|%")
AND t.colA NOT LIKE "%3%"    
AND t.colA NOT LIKE "%|2|%" 
AND t.colA NOT REGEXP "^2|%" 
AND t.colA NOT REGEXP "%|2$"

      

There may be a more elegant way to do this, but that does the trick.

+3


source to share


1 answer


How about using a function LIKE

?

where (t like  '%4%' or t like  '%5%')
and (t not like  '%2%' and t not like  '%3%')

      



This should do the job.

+3


source







All Articles