【问题标题】:Presto SQL query快速 SQL 查询
【发布时间】:2022-11-29 22:43:24
【问题描述】:

假设我有一个具有以下值的字符串数组:

string = {'123','12ab','38','abc','01a8','1123b'}

我应该如何在 Presto SQL 中进行查询以仅提取仅包含且仅包含数字的值,以便我的输出为 {'123','38'}?

执行类似下面的查询,不会返回任何输出

SELECT string
FROM table1
WHERE string LIKE '[0-9]*'
GROUP BY string

我的数据样本示例 enter image description here

【问题讨论】:

  • string = {'123','12ab','38','abc','01a8','1123b'} 在我看来不像数组。数据中有 json 数组吗?您能否发布一些实际示例,即select string from table1 中的几行?
  • 抱歉,这只是一个例子。所以我正在将数据从一个变量检索到一个列......假设该列具有以下值 COLUMN1 123 12ab 38 abc 01a8 1123b 所以我的问题是:如果我想从中获取值COLUMN1 只包含数字,我怎么能在 presto sql 查询中做到这一点?那有可能吗?
  • 我用我的数据图片更新了我的问题

标签: sql regex presto apache-superset


【解决方案1】:

至少有两个选择:

  1. 利用 Presto 提供的 try_cast 运算符

    -- sample data
    WITH dataset(string) AS (
      values ('123'),
             ('12ab'),
             ('38'),
             ('abc'),
             ('01a8'),
             ('1123b')
     )
    
     -- query
     select *
     from dataset
     where try_cast(string as integer) is not null;
    

    或者通过regexp_like使用正则表达式:

     -- query
     select *
     from dataset
     where regexp_like(string, '^d+$');
    

    输出:

    string
    123
    38

【讨论】:

    猜你喜欢
    • 2013-08-31
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2022-01-19
    • 2011-06-08
    • 2020-01-30
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多