如何根据 Postgres 中的年份获取财政年度月份(四月至三月)

问题描述 投票:0回答:1

我需要根据 Postgres 查询中的年份获取财政年度月份(四月至三月)...

就像如果过了 2022 年,那么我需要获取(12 个月列表)2022 年 4 月到 2023 年 3 月的月份列表。

我尝试使用 EXTRACT() 函数日期到年份,但没有得到确切的结果。

postgresql pgadmin
1个回答
0
投票
SELECT to_char(t, 'Month-yyyy') AS financial_month
from generate_series(
             date 'yyyy-04-01'::date, -- Replace yyyy with your desired year
             date 'yyyy-04-01'::date + interval '1 year - 1 day',
             interval '1 month'
         ) t

此查询生成一系列从指定年份 4 月 1 日开始到下一年(财政年度)3 月 31 日结束的日期。然后,它使用 to_char 函数将这些日期格式化为“Month-YYYY”(例如“2022 年 4 月”、“2022 年 5 月”、...、“2023 年 3 月”)。

© www.soinside.com 2019 - 2024. All rights reserved.