用于获取给定列的每个可能值的项目数量的 SQL 查询
好的,我有一个名为 log 的 (SQLite) 表,其中一列(时间)是 Unix 纪元时间戳(其他列与我的目的无关)。这些是带时间戳的日志条目。
我试图计算每小时记录的项目数(所有时间 - 我不在乎只在某一天或其他什么时候这样做;我试图了解整个过程中的活动密度) 平均天)。我已经成功地使用查询 SELECT STRFTIME('%H', DATETIME(time, 'unixepoch', 'localtime')) FROM log; 基本上将这些时间分类为几个小时;,然后它给了我一个包含 0-23 数字的巨大列表,我可以通过 uniq -c
运行它来获取每小时的事件计数。
然而(我承认更多的是出于固执而不是任何实际需要),我想在 SQL 中执行这个计数步骤。我的大脑让我知道我可能必须进行某种自连接才能仅获取生成的小时数列的唯一值,但这就是它产生的结果 D:有什么想法吗?
Okay, so I have a (SQLite) table called log wherein one column (time) is a Unix epoch timestamp (the other columns are irrelevant for my purposes). These are timestamped log entries.
I am trying to get a count of how many items are logged per hour (all-time -- I don't care to do it only for a given day or whatever; I'm trying to get an idea of activity density throughout the average day). I have been successful in basically binning these into hours with the query SELECT STRFTIME('%H', DATETIME(time, 'unixepoch', 'localtime')) FROM log;
, which then gives me just a huge list of numbers 0-23 which I can run through uniq -c
to get a count of events per hour.
However (more out of stubbornness than any practical need, I admit), I want to do this counting step in SQL. My brain got me as far as knowing I'd prrroooobably have to do some kind of self-join to get only the unique values for the generated hours column, but that's where it crapped out D: Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:我没有使用过 SQLite。但是,它支持
GROUP BY
,因此,这应该可以工作。Note: I haven't worked with SQLite. However, it supports
GROUP BY
and hence, this should work.为什么使用 COUNT(anotherField)?
使用
SELECT [您的表达式], COUNT(*) FROM log GROUP BY [您的表达式]
why use COUNT(anotherField)?
Use
SELECT [your expression], COUNT(*) FROM log GROUP BY [your expression]