我有一个看起来像这样的表:
I have a table that looks like this:
FOO BAR BAZ ----+----+---- foo1 bar1 baz1 foo2 bar3 baz2 foo3 bar1 baz3 foo4 bar1 baz4 foo5 bar3 baz5 foo6 bar1 baz6 foo7 bar2 baz7因此,我想获得每个小节出现在表格中的次数..因此,我正在寻找的输出看起来像这样:
And as a result I would like to get the count of how many times each bar appeared in the table.. so, the output I'm looking for looks like this:
BAR COUNT -----+----- bar1 4 bar3 2 bar2 1我可以在SQLite中查询类似的内容吗?我想这应该很简单,但是无论如何我都不是SQL程序员,我只需要将此简单查询作为python脚本的一部分即可.谢谢.
Can I do a query for something like this in SQLite? I guess it should be pretty easy, but I am not an SQL programmer by any means, and I just need this simple query as a part of a python script.. Thank you.
推荐答案SELECT foo, count(bar) FROM mytable GROUP BY bar ORDER BY count(bar) DESC;
group by语句告诉聚合函数按列对结果集进行分组.在这种情况下,按栏分组"表示按栏的不同类型"对栏列中的字段数进行计数.
The group by statement tells aggregate functions to group the result set by a column. In this case "group by bar" says count the number of fields in the bar column, grouped by the different "types" of bar.
更好的解释在这里: www.w3schools/sql/sql_groupby.asp
更多推荐
SQLite计数,按计数分组和排序
发布评论