我正在编写一个查询来获取三个不同时间间隔内两个日期之间的差异。
ID , D1 , D2
1 , '2013-01-01' , '2013-01-01'
2 , '2013-01-01' , '2013-02-01'
3 , '2013-01-01' , '2013-03-01'
4 , '2013-01-01' , '2013-07-01'
我想要三类结果:第一周、一个月、然后 3 个月、6 个月、9 个月等。
case when round((DATEDIFF(D2,D1))/7,0) < 1 then '1st week'
when round((DATEDIFF(D2,D1))/7,0) >1 and round((DATEDIFF(D2,D1))/7,0) <5 then 'one month'
when ????
剩下的我已经想不出来了。
最佳答案
when YEAR(D2)*4+QUARTER(D2)-YEAR(D1)*4-QUARTER(D1)=1 then 'one quarter'
然后您可以相应地计算季度 3、6、9 个月 = 1、2、3 个季度
关于python - 不同间隔的日期差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309218/