Impala Query: Find value in pipe-separated list

可紊 提交于 2020-01-04 01:18:23

问题


I have a column containing rows of pipe separated 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 that contain 4 or 5, but never 2 or 3. Something along the lines of:

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

Resulting in:

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

I ended up using a combination of the two answers below, as using either method alone still left me with rows containing only "255". Here's the final query:

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 is probably a more elegant way to do this, but that does the trick.


回答1:


What about using the LIKE function ?

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

That should do the job.



来源:https://stackoverflow.com/questions/31173825/impala-query-find-value-in-pipe-separated-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!