首页 > 解决方案 > 包含 presto Athena 中的字符串函数

问题描述

ORC在雅典娜与 Serde 有一张桌子。该表包含一个名为 的字符串列greeting_message。它也可以包含值。我想找出表格中有多少行具有特定文本作为模式。

假设我的示例数据如下所示:

|greeting_message |
|-----------------|
|hello world      |
|What's up        |
|                 |
|hello Sam        |
|                 |
|hello Ram        |
|good morning, hello |
|                 |
|the above row has null |
| Good morning Sir |

现在对于上表,如果我们看到总共有 10 行。其中 7 个没有空值,其中 3 个只有空/空值。

我想知道有多少行包含特定单词。

例如,考虑单词hello。它存在于 4 行中,因此此类行的百分比为 4/10,即 40%。

另一个例子:这个词morning出现在 2 条消息中。因此,此类行的百分比为 2/10,即 20%。

请注意,我null也在考虑分母的数量。

标签: mysqlcontainsamazon-athenaprestostring-function

解决方案


SELECT SUM(greeting_message LIKE '%hello%') / COUNT(*) AS hello_percentage, 
       SUM(greeting_message LIKE '%morning%') / COUNT(*) AS morning_percentage 
FROM tablename

推荐阅读