SQL查询中的总单词计数



我有一个带有2个字段的表:

cnt  str
--  -------
60   the
58   of
4    no
30   the
2    of
1    no

我想要这样的结果

cnt  str
--  -------
90   the
60   of
5    no

我将如何编写一个查询以在表下面喜欢?

SELECT str, 
SUM (cnt) 
FROM table_name
GROUP BY str;

这将按str对表格进行分组,即所有这些都将在一起,然后求和,依此类推。如果您想重命名总和(CNT)使用:

SELECT str, 
SUM (cnt) as cnt
FROM table_name
GROUP BY str;

使用 GROUP BY

select sum(cnt) as cnt, str from my_table group by str

这确实可以起作用,因为它按照您想要的是STR分组的CNT分组:

select sum(cnt),str from tablename group by str;