SQLite 计数、分组和按计数排序
我有一个如下所示的表格:
FOO BAR BAZ
----+----+----
foo1 bar1 baz1
foo2 bar3 baz2
foo3 bar1 baz3
foo4 bar1 baz4
foo5 bar3 baz5
foo6 bar1 baz6
foo7 bar2 baz7
因此,我想获取每个条形在表格中出现的次数。所以,我正在寻找的输出如下所示:
BAR COUNT
-----+-----
bar1 4
bar3 2
bar2 1
我可以进行查询吗SQLite 中有这样的东西吗?我想这应该很简单,但我无论如何都不是 SQL 程序员,我只需要这个简单的查询作为 python 脚本的一部分。谢谢。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
group by 语句告诉聚合函数按列对结果集进行分组。在本例中,“按条形分组”表示计算条形列中的字段数量,并按条形的不同“类型”进行分组。
更好的解释在这里:http://www.w3schools.com/sql/sql_groupby.asp
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.
A better explanation is here: http://www.w3schools.com/sql/sql_groupby.asp