繁体   English   中英

提取与租赁期重叠的财政年度

[英]Extracting financial years overlapping with tenancy periods

鉴于这些租赁合同:

          2012        2013        2014        2015        2016
YR        |           |           |           |           |     
FIN_YR       | 2012-2013 | 2013-2014 | 2014-2015 | 2015-2016 |     
          ____________________________________________________
1         ----------------++++--------------------------------
2         -----+++++++++++++++++++++++++++++++++++++++--------
4         -----------------------------++++++++++++++++++-----

持续了这些日期:

TENANCY_ID FROM       TO
---------- ---------- ----------
1          2013-05-02 2013-08-12
2          2012-06-22 2015-09-01
4          2014-06-03 2015-11-15

我想制作一张长桌,例如:

TENANCY_ID Financial_Year
---------- --------------
1          2013-2014
2          2012-2013
2          2013-2014
2          2014-2015
2          2015-2016
4          2014-2015
4          2015-2016

其中, Financial_Year显示每个租约(至少部分持续)的财政年度(4月1日至3月31日)。

如果相关,请使用 ,否则可以使用通用解决方案。

抱歉,还没有db2,这是Oracle的示例:

with financial_years as (
      select to_char(r) || '-' || to_char(r + 1) as year, 
             to_date('01.04.' || to_char(r),'dd.mm.yyyy') as date_begin, 
             to_date('31.03.' || to_char(r + 1) || '23:59:59','dd.mm.yyyy hh24:mi:ss') as date_End
      from t_fin_year -- here's a table (year INT)
    )
    select y.year,
           t.id
    from t_tenancy t
    join financial_years y
      on y.date_begin between t.from and t.to
         OR y.date_end between t.from and t.to
    order by t.id, y.year;

主要思想是使用租期开始日期加入财务年度:如果年份开始或结束在租期开始\\结束之间,则租期属于今年。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM