如何在蜂巢中计算字数

问题描述

我有一个table_a的桌子

query
------
apple
no fruit
this is 
not a number

我想编写配置单元查询获取基于空格的单词计数

query           count
------      ------------
apple           1
no fruit        2
this is         2
not a  number   3

解决方法

一种方法是拆分为数组并计算元素:

select t.*,size(split(query,' '))

如果要将多个空格作为单个定界符进行计数:

select t.*,'[ ]+'))
,

您需要拆分列内容,例如

select  *,'[\\s]+'))  as count
from table_a

使用空格\s速记字符不仅可以忽略多个空格,还可以忽略制表符等。