简体   繁体   中英

How to get part of the String before last delimiter in AWS Athena

Suppose I have the following table in AWS Athena

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

I need to extract the part of the string from columns before last delimiter(Here '-' is delimiter)

Basically need a query which can give me output as

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

Also i need a group by query which ca generate this


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

I tried various forms of MySql queries using LEFT(), RIGHT(), LOCATE(), SUBSTRING_INDEX() functions but it seems that athena does not support all these functions.

You could use regexp_replace() to remove the part of the string that follows the last '-' :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM