longest matching prefix

2019-04-13 17:27发布

In MySQL to match '12684041234' to the longest prefix you would do

SELECT num_prefix
FROM nums
WHERE '12684041234' LIKE CONCAT(num_prefix, '%')
AND LENGTH(num_prefix) = (
    SELECT MAX(LENGTH(num_prefix))
    FROM nums
    WHERE '12684041234' LIKE CONCAT(num_prefix, '%')
)

Table nums has a column named num_prefix with prefix values.

How can I do it in hive ?

1条回答
Melony?
2楼-- · 2019-04-13 18:03

This is how I do it in MySQL:

SELECT num_prefix FROM nums
  WHERE '12684041234' LIKE CONCAT(num_prefix,'%')
  ORDER BY num_prefix DESC
  LIMIT 1
;

This will give the longest prefix (ORDER BY .. DESC) and only one row (LIMIT 1).

查看更多
登录 后发表回答