【问题标题】:impala sql only choose numbers of certain lengthimpala sql 只选择一定长度的数字
【发布时间】:2018-04-29 22:09:01
【问题描述】:

我有一个用作 free-txt 字段的变量,有数千行长。

虽然它应该只包含帐号,但它还包含电话号码、文本或 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

【问题讨论】:

    标签: sql cloudera impala


    【解决方案1】:

    有趣。我会使用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;
    

    【讨论】:

      【解决方案2】:

      正则表达式对此很有用。试试:

      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
      

      您可能需要将计数转换为浮点数才能使除法起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-05
        • 2018-10-25
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        • 2015-02-23
        • 1970-01-01
        • 2023-03-11
        相关资源
        最近更新 更多