【问题标题】:How to get part of the String before last delimiter in AWS Athena如何在 AWS Athena 中的最后一个分隔符之前获取部分字符串
【发布时间】:2020-05-21 13:53:35
【问题描述】:

假设我在 AWS Athena 中有下表

+----------------+
|     Thread     |
+----------------+
| poll-23        |
| poll-34        |
| pool-thread-24 |
| spartan.error  |
+----------------+

我需要从最后一个分隔符之前的列中提取字符串的一部分(这里'-'是分隔符)

基本上需要一个可以给我输出的查询

+----------------+
|     Thread     |
+----------------+
| poll           |
| poll           |
| pool-thread    |
| spartan.error  |
+----------------+

我还需要一个可以生成这个的查询分组


+---------------+-------+
|    Thread     | Count |
+---------------+-------+
| poll          |     2 |
| pool-thread   |     1 |
| spartan.error |     1 |
+---------------+-------+

我使用 LEFT()、RIGHT()、LOCATE()、SUBSTRING_INDEX() 函数尝试了各种形式的 MySql 查询,但似乎 athena 不支持所有这些函数。

【问题讨论】:

    标签: sql regex string presto amazon-athena


    【解决方案1】:

    您可以使用regexp_replace() 删除最后一个'-' 之后的字符串部分:

    select regexp_replace(thread, '-[^-]*$', ''), count(*) 
    from mytable
    group by regexp_replace(thread, '-[^-]*$', '')
    

    【讨论】:

    • 第一行好像有错字。当我在(thread, '-[^-]*$'; '') 中将; 替换为, 时,它对我有用
    猜你喜欢
    • 2020-09-13
    • 2013-03-28
    • 2012-02-06
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多