sqlite - 按时间戳分组 sqlite3
问题描述
我有一个具有如下时间戳的列。我想按月份分组。我正在使用下面的查询。但在大型数据集中,它有时会给出错误的结果。我想说的是在那个时间戳中使用 group by (每月)的任何其他方式吗?
SELECT count(distinct a) from abc GROUP BY (select strftime('%m', timestamp) as valMonth) where col1 like '%a%'
timestamp a co1
2020-01-23 17:22:57 12 a
2020-01-23 17:26:59 12 a
2020-02-23 17:26:59 13 b
2020-02-23 17:26:59 13 b
2020-03-23 17:26:59 14 a
2020-03-23 17:26:59 14 a
解决方案
如果您使用:
strftime('%m', timestamp)
那么你只按月分组,所以结果将是例如所有年份的所有 12 月的 1 个组。
您应该按年份和月份分组:
strftime('%Y-%m', timestamp)
因此,将您的查询更改为:
SELECT strftime('%Y-%m', timestamp) AS YearMonth,
...............
FROM abc
GROUP BY YearMonth
推荐阅读
- node.js - Google Sheets API 导致 Node 中出现“GoogleAuth 不是构造函数”错误
- c++ - 如何将位置存储在数组中并用于以后在 C++ 中?
- jsf - 通过未封装的selectOneMenu显示表单
- python-3.x - TypeError:不能在类似字节的对象正则表达式上使用字符串模式,没有获得标准
- javascript - 在 React 中,是否可以更改 map 方法内部的状态,如果不能,有什么替代方法?
- python - 如果子表中的值发生变化,则更新父表的最后修改时间
- tensorflow-datasets - tensorflow 数据集找到了不同版本的请求数据集
- matlab - 预分配单个单元格的内存
- r - 在ggplot R中创建自定义/更多刻度x轴距离
- java - 通过 split() 解析文件名以比较两个常用字段