基于会计年度的日期分配 [英] Date split-up based on Fiscal Year
问题描述
给定从日期,到日期和会计年度系统,我想获得$的所有分割时间b $ b从&基于会计年度系统的日期。下面用示例说明:
示例1:
会计年度系统:Apr to Mar
自Date:Jan-05-2008
到日期:May-15-2008
根据会计年度系统,分成:
2008年1月5日 - 2008年3月-3月
2008年4月 - 15-2008
示例2:
会计年度系统: 4月至3月
自日期:2008年1月17日
到日期:2009年5月20日
根据会计年度系统,期限应分为:
2008年1月17日至2008年3月31日
2009年4月 - 2008年4月 - 2009年3月31日
2009年4月 - 2009年5月 - 2009年5月20日
我在寻找在PostgreSQL 8.2中解决这个问题的方法/算法。
注意,
Gnanam
CREATE TABLE your_table(start_date date,end_date date);
INSERT INTO your_table VALUES('Jan-17-2008','May-20-2009');
SELECT
GREATEST(start_date,('04 -01-'|| series.year):: date)AS year_start,
LEAST(end_date, - '|| series.year + 1):: date)AS year_end
FROM
(SELECT
start_date,
end_date,
generate_series(
date_part ('year',your_table.start_date - INTERVAL'3 months'):: int,
date_part('year',your_table.end_date - INTERVAL'3 months'):: int)
from your_table) AS系列(start_date,end_date,year)
ORDER BY
start_date;
Given a From Date, To Date and a Fiscal Year system, I want to get all the split-up duration within the given From & To Date based on the Fiscal Year system. Explained below with examples:
Example 1: Fiscal Year system: Apr to Mar
From Date: Jan-05-2008 To Date: May-15-2008
Based on Fiscal Year system, duration should be splitted into:
Jan-05-2008 to Mar-31-2008
Apr-01-2008 to May-15-2008
Example 2: Fiscal Year system: Apr to Mar
From Date: Jan-17-2008 To Date: May-20-2009
Based on Fiscal Year system, duration should be splitted into:
Jan-17-2008 to Mar-31-2008
Apr-01-2008 to Mar-31-2009
Apr-01-2009 to May-20-2009
Am looking for approach/algorithm to solve this in PostgreSQL 8.2.
Regards,
Gnanam
I actually favor Andomar's solution (with the addition of a processes that automatically fills the Periods table), but for fun here's a solution that doesn't require it.
CREATE TABLE your_table (start_date date, end_date date);
INSERT INTO your_table VALUES ('Jan-17-2008', 'May-20-2009');
SELECT
GREATEST(start_date, ('04-01-'||series.year)::date) AS year_start,
LEAST(end_date, ('03-31-'||series.year + 1)::date) AS year_end
FROM
(SELECT
start_date,
end_date,
generate_series(
date_part('year', your_table.start_date - INTERVAL '3 months')::int,
date_part('year', your_table.end_date - INTERVAL '3 months')::int)
FROM your_table) AS series(start_date, end_date, year)
ORDER BY
start_date;
这篇关于基于会计年度的日期分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!