计算同比增长 SQL Server 2008R2

Calculate year over year increase SQL Server 2008R2

下面是我的 table 结构。我需要计算每个房产的租期租金:

我们来看PropertyID = 12077:

由于此 属性 租约在 2023 年和 2028 年之间开始和结束,我想知道(每年单独一行)每年要收取的租金数额。这将考虑每 12 个月增加的百分比(复合租金增加)。

示例:
21.53 * 1280 将提供前 12 个月的租金。但是,租约于 2 月开始,因此 2023 年的总租金金额为 = ((21.23 * 1280)/12) * 11

对于 2024 年,第一个月的租金将为 = (21.23 * 1280)/12,因为租金每 12 个月才增加一次。对于 2024 年接下来的 11 个月,租金将为 ((12.23 * 1.04 * 1280)/12)* 11.

对于 2025 年,第一个月的租金将为 (12.23 * 1.04 *1280)/12)。但是,2025 年接下来的 11 个月将是 ((12.72 * 1.04 * 1280)/12)*11。 12.72来自复合增长。

我将如何着手提出一个观点来做到这一点?最让我困惑的部分是不知道如何适应租赁开始日期,因为租赁开始日期不是一月份开始的。


declare @table table 
(
    PropertyID int,
    area int,
    StartDate date,
    EndDate date,
    BaseRent decimal(12,2),
    RentIncreaseBasis varchar(30),
    RentIncreasePercent decimal(5,2),
    IncreaseRepeatMonths int

)

insert @table values (12076,    5627,   '2024-01-01',   '2028-12-31',   '16.52',    '% Increase',   0.03, 12)
insert @table values (12077,    1280,   '2023-02-01',   '2027-10-31',   '21.53',    '% Increase',   0.04, 12)
insert @table values (12078,    1000,   '2017-03-01',   '2025-11-30',   '23.52',    '% Increase',   0.01, 12)
insert @table values (12079,    2000,   '2020-02-01',   '2024-09-30',   '15.57',    '% Increase',   0.05, 12)
insert @table values (12080,    3000,   '2018-05-01',   '2020-08-31',   '18.58',    '% Increase',   0.04, 12)
insert @table values (12081,    4000,   '2019-08-01',   '2020-12-31',   '22.56',    '% Increase',   0.03, 12)
insert @table values (12082,    5000,   '2017-02-01',   '2028-03-31',   '19.53',    '% Increase',   0.02, 12)

select * from @table

我建议使用日历 table,其中包含您 table 中的所有月份。 我希望我的例子能在 SQL 2008 年发挥作用。

-- here is your code

-- the calendar table
DECLARE @MonthCalendar table(
  [Month] date PRIMARY KEY
)

DECLARE @MinDate date,@MaxDate date

-- get min and max date
SELECT
  @MinDate=MIN(StartDate),
  @MaxDate=MAX(EndDate)
FROM @table

-- fill the calendar table
;WITH monthCTE AS(
  SELECT CAST(@MinDate AS date) [Month]

  UNION ALL

  SELECT DATEADD(MONTH,1,[Month])
  FROM monthCTE
  WHERE [Month]<@MaxDate
)
INSERT @MonthCalendar([Month])
SELECT [Month]
FROM monthCTE
OPTION(MAXRECURSION 0);

-- final query
SELECT
  *,
  (BaseRent*Area*(1+RentIncreasePercent*IncreaseCount))/12 MonthRentAmount,
  (1+RentIncreasePercent*IncreaseCount) TotalPercent
FROM
  (
    SELECT *,(ROW_NUMBER()OVER(PARTITION BY t.PropertyID ORDER BY m.[Month])-1)/12 IncreaseCount
    FROM @table t
    JOIN @MonthCalendar m ON m.[Month] BETWEEN t.StartDate AND t.EndDate
    --WHERE t.PropertyID=12077
  ) q

-- query for total amounts by PropertyIDs and Years
SELECT
  PropertyID,
  YEAR(StartDate) [Year],
  SUM((BaseRent*Area*(1+RentIncreasePercent*IncreaseCount))/12) YearRentAmount
FROM
  (
    SELECT *,(ROW_NUMBER()OVER(PARTITION BY t.PropertyID ORDER BY m.[Month])-1)/12 IncreaseCount
    FROM @table t
    JOIN @MonthCalendar m ON m.[Month] BETWEEN t.StartDate AND t.EndDate
    --WHERE t.PropertyID=12077
  ) q
GROUP BY PropertyID,YEAR(StartDate)
ORDER BY PropertyID,[Year]