【问题标题】:JOOQ : Get fiscal quarter with custom month rangeJOOQ:获取具有自定义月份范围的财政季度
【发布时间】:2022-01-18 14:30:38
【问题描述】:

https://www.jooq.org/doc/latest/manual/sql-building/column-expressions/datetime-functions/quarter-function/

这适用于正常季度。如果我想让它适用于具有自定义月份的财政季度怎么办?


财政季度意味着.. 可以是任何月份,而不是作为第一季度的开始月份的一月。

如果我认为 8 月是我会计年度的第一个月。

我的财政季度如下所示:

Aug-Sep-Oct'2021 => 1st fiscal quarter '2021
Nov-Dec-Jan'2022 => 2nd fiscal quarter '2021
Feb-March-April'2022 => 3rd fiscal quarter '2021
May-June-July'2022 => 4th fiscal quarter '2021

【问题讨论】:

  • 正式的“财政季度”是什么意思?
  • @LukasEder 更新了问题描述

标签: jooq


【解决方案1】:

如果您的财务季度严格按月计算,为什么不对DSL.month(date) 进行一些算术运算,例如:

trunc(month(DATE_COLUMN).add(4).div(3)).mod(4).add(1)

PostgreSQL 中的一个例子:

select d, trunc((extract(month from d) + 4) / 3) % 4 + 1
from (
  select (date '2000-01-01' + i * interval '1 month')::date
  from generate_series(0, 11) as t(i)
) t(d)

导致

|d         |?column?|
|----------|--------|
|2000-01-01|2       |
|2000-02-01|3       |
|2000-03-01|3       |
|2000-04-01|3       |
|2000-05-01|4       |
|2000-06-01|4       |
|2000-07-01|4       |
|2000-08-01|1       |
|2000-09-01|1       |
|2000-10-01|1       |
|2000-11-01|2       |
|2000-12-01|2       |

dbfiddle here。现在,根据您会计年度的第一个月,通过切换add(4) 值相应地调整公式。

【讨论】:

  • 我想要的那种。但我真正想要的是我的第二个财政季度由 2000 年 11 月、2000 年 12 月和 2001 年 1 月组成.....
  • @moovon:你大概可以从这里拿走吧?将修补后的年份添加到季度将以相同的方式工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多