KDB+:获取字符串表中所有子字符串的计数



我是KDB的新手,正在努力创建查询。谢谢你的帮助。

我有一个字符串表,需要得到表中所有字符串中所有特定子字符串的计数。

那么,我们假设有字符串:

[ 
string1: Apple is green, cherry is red, 
string2: Ququmber is green, banana is yellow 
] 

,我想得到"苹果"one_answers";green"跨所有子字符串。我想要的结果是有一个这样的分组:

{ 
Apple: 1, 
green: 2 
} 

但是,不幸的是,我不知道如何进行这样的分组。我已经弄清楚了如何获得包含至少一个所需子字符串的字符串:

"select count(text) from data where any text like/: ("*$Apple*";"*$green*")"

但是它返回给我的是Apple和green所有字符串的累积结果,没有任何分组:

{
text: 3
}

不允许区分每个特定子字符串的数量。

我将非常感谢任何帮助。

不使用带有any的where子句,您可以将like/:放在select短语中以获得嵌套的布尔值列表,其中每个列表表示一个搜索字符串的匹配项。然后,您只需sum这些来获得每个搜索字符串的总匹配项。我在这里使用了exec而不是select,因为我怀疑输出将更有用:

q)t:([] text:("Apple is green, cherry is red,";"Ququmber is green, banana is yellow"))
q)exec sum each text like/:("*Apple*";"*green*") from t
1 2i

您可以使用-4!来计算每个子字符串的频率

q)t:([] text:("Apple is green, cherry is red,";"Ququmber is green, banana is yellow"))
q)count each group exec raze -4!'text from t
"Apple"   | 1
," "      | 10
"is"      | 4
"green"   | 2
,","      | 3
"cherry"  | 1
"red"     | 1
"Ququmber"| 1
"banana"  | 1
"yellow"  | 1

最新更新