SQLite计数,按计数分组和排序

编程入门 行业动态 更新时间:2024-10-10 05:24:06
本文介绍了SQLite计数,按计数分组和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个看起来像这样的表:

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计数,按计数分组和排序

本文发布于:2023-10-30 07:39:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!