impala sql 只选择一定长度的数字
impala sql only choose numbers of certain length
我有一个用作免费 txt 字段的变量,有数千行长。
虽然它应该只包含帐号,但它还包含 phone 个数字、文本或 NULL。
我只需要提取包含帐号的列(8 位数字字段)。
我如何将其存档在 SQL impala 中,尤其是因为我们不仅有数字,还有文本。
我还需要知道帐号与其他帐号的百分比,以估计修复其他字段所需的时间。
如何才能做到这一点?
它看起来像这样:
accounts
---------
12345678
23456789
test only
34567890
23443256
23443257
021735547
23443258
23443259
23443260
call back
23443261
53443262
23443263
23443264
23443265
cancel
53443262
53443263
63443264
53443265
73443266
53443267
正则表达式对此很有用。尝试:
select regexp_extract(free_text_column, '^[0-9]{8}$',1) from your_table
要获得你能做到的百分比
select count(regexp_extract(free_text_column, '^[0-9]{8}$',1))/count(*)
from your_table
为了使除法正常工作,您可能需要将计数转换为浮点数。
有意思。我会使用 regexp_like()
:
select sum(case when regexp_like(col, '^[0-9]{8}$') then 1 else 0 end) as cnt,
avg(case when regexp_like(col, '^[0-9]{8}$') then 1.0 else 0 end) as ratio
from t;
我有一个用作免费 txt 字段的变量,有数千行长。
虽然它应该只包含帐号,但它还包含 phone 个数字、文本或 NULL。
我只需要提取包含帐号的列(8 位数字字段)。 我如何将其存档在 SQL impala 中,尤其是因为我们不仅有数字,还有文本。 我还需要知道帐号与其他帐号的百分比,以估计修复其他字段所需的时间。 如何才能做到这一点? 它看起来像这样:
accounts
---------
12345678
23456789
test only
34567890
23443256
23443257
021735547
23443258
23443259
23443260
call back
23443261
53443262
23443263
23443264
23443265
cancel
53443262
53443263
63443264
53443265
73443266
53443267
正则表达式对此很有用。尝试:
select regexp_extract(free_text_column, '^[0-9]{8}$',1) from your_table
要获得你能做到的百分比
select count(regexp_extract(free_text_column, '^[0-9]{8}$',1))/count(*)
from your_table
为了使除法正常工作,您可能需要将计数转换为浮点数。
有意思。我会使用 regexp_like()
:
select sum(case when regexp_like(col, '^[0-9]{8}$') then 1 else 0 end) as cnt,
avg(case when regexp_like(col, '^[0-9]{8}$') then 1.0 else 0 end) as ratio
from t;