1

例如,如果我有 1000 行数据,其中包含客户 ID(例如 123)以及他们对我们产品的评论(例如易于使用的出色产品)

如何使用 Teradata(版本 15)进行字频计数,以便输出有两列,一列是单词,另一列是频率,例如(Great:20,Product:10)?

谢谢

4

1 回答 1

3

你可以用它strtok_split_to_table来解决这个问题。

类似于以下内容:

SELECT d.token, SUM(d.outkey)
FROM TABLE (strtok_split_to_table(1, <yourtable>.<yourcommentsfield>, ' ')
        RETURNS (outkey integer, tokennum integer, token varchar(20)character set unicode) ) as d 
GROUP BY 1

这会将评论字段中的每个单词拆分为单独的记录,然后计算每个单词的出现次数。把你自己的<yourtable>.<yourcommentsfield>放在那里,你应该很高兴。

关于 strtok_split_to_table 的更多信息:http: //www.info.teradata.com/HTMLPubs/DB_TTU_14_00/index.html#page/SQL_Reference/B035_1145_111A/String_Ops_Funcs.084.242.html

这是在我的系统上进行测试的 SQL 和结果:

CREATE SET TABLE db.testcloud ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      customer VARCHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC,
      comments VARCHAR(1000) CHARACTER SET LATIN NOT CASESPECIFIC)
PRIMARY INDEX ( customer );


INSERT INTO testcloud (1, 'This is a test comment');
INSERT INTO testcloud (2, 'This is also comment of something');

SELECT d.token, SUM(d.outkey)
FROM TABLE (TD_SYSFNLIB.strtok_split_to_table(1, testcloud.comments, ' -/')
        RETURNS (outkey integer, tokennum integer, token varchar(20)character set unicode) ) as d 
GROUP BY 1

--token Sum(outkey)
--is    2
--also  1
--This  2
--of    1
--test  1
--a 1
--comment   2
--something 1
于 2015-04-01T16:17:31.530 回答