SQLite计数,按计数分组和排序 [英] SQLite count, group and order by count
问题描述
我有一个看起来像这样的表:
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.
更好的解释在这里: http://www.w3schools.com/sql/sql_groupby.asp
这篇关于SQLite计数,按计数分组和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!