如何在 SQLite 中按季度/3 个月周期(或任何自定义时间段)分组



这是我以逗号分隔的形式提供的表格:

date, number
2010-09-02, 2
2010-10-01, 3
2011-01-01, 4
2011-02-01, 5
2011-03-01, 6
2011-05-05, 7

SQLite 的语法中似乎没有"四分之一"日期时间函数,所以我想知道是否有解决方法。

(cast(strftime('%m', date) as integer) + 2) / 3 as quarter
CASE 
  WHEN cast(strftime('%m', date) as integer) BETWEEN 1 AND 3 THEN 1
  WHEN cast(strftime('%m', date) as integer) BETWEEN 4 and 6 THEN 2
  WHEN cast(strftime('%m', date) as integer) BETWEEN 7 and 9 THEN 3
  ELSE 4 END as Quarter

抱歉,举个例子。

select month, (((3-(month%3))%3)+month)/3 as 'qtr' from months

会给我们:

1   1
2   1
3   1
4   2
5   2
6   2
7   3
8   3
9   3
10  4
11  4
12  4

使用一些模数来解决它怎么样?

(((3-(month%3))%3)+month)/3

其中 month 是介于 1-12 之间的数字列

基本上,我只是将事情四舍五入到最接近的 3,然后将其潜水 3。

相关内容

最新更新