如何在SQLite中按季度/3个月(或任何自定义时间段)分组?
这是我的以逗号分隔的表格:如何在SQLite中按季度/3个月(或任何自定义时间段)分组?,sqlite,datetime,date,Sqlite,Datetime,Date,这是我的以逗号分隔的表格: 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的语法中似乎没有“quarter”datetime函数,所以我想知道是否有解决方法。使用一些模来解决它怎么样 CASE WHEN cast(strftime('%m', date) as integer) BETWEEN 1 AND 3 THEN 1 W
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的语法中似乎没有“quarter”datetime函数,所以我想知道是否有解决方法。使用一些模来解决它怎么样
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
(((3-(month%3))%3)+month)/3
其中月份是一列介于1-12之间的数字
基本上,我只是把事情四舍五入到最接近的3,然后再按3进行细分。对不起,举个例子
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
也可以在不使用铸造的情况下工作。不使用铸造的代码:
(strftime('%m',date)+2)/3作为季度
1 1
2 1
3 1
4 2
5 2
6 2
7 3
8 3
9 3
10 4
11 4
12 4