【问题标题】:Calculate year over year increase SQL Server 2008R2计算 SQL Server 2008R2 的同比增长
【发布时间】:2017-11-14 20:37:16
【问题描述】:

下面是我的表结构。我需要计算每个物业的租期租金:

我们看一下 PropertyID = 12077:

  • 面积 = 1280
  • 开始日期 = 2023-02-01
  • 结束日期 = 2027-10-31
  • BaseRent = 21.53
  • RentIncreasePercent = .04 (4%)
  • IncreaseRepeatMonths = 12 个月(注意:前 12 个月不会增加)

由于该物业租约在 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

【问题讨论】:

  • 你打算用纯 sql(不是 t-sql)吗?
  • 我认为您需要做的是将您的日期范围分解为一组带有日历年和“租赁”年的月份。因此,您的开始日期是第 2 个月,日历年 2023,租约年 0。下一行第 3 个月,cal yr 2023,租约 yr 0。第 12 行是第 1 个月(1 月),cal yr 2024,租约 yr 0,第 13 行是月份2,cal yr 2024,lease yr 1。然后,您应该拥有将日历和“租赁”月份分开所需的数据,用作租金增长的乘数。
  • @SmartDumb 我有点需要它以将其插入另一个存储过程逻辑。但是,如果有性能,t-sql 也可以。归根结底,数据用于复杂得多的 ssrs 报告。
  • @beth 让我试试这个,看看效果如何。

标签: sql sql-server-2008-r2 calculated-columns


【解决方案1】:

我建议使用包含表中所有月份的日历表。 我希望我的示例适用于 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]

【讨论】:

  • 这非常有效!我只需要更新 YearRentAmount 公式。你所拥有的不是计算复合率。
  • 公式如下:(BaseRent * Area * (Power(cast(1 + RentIncreasePercent as float) ,IncreaseCount)))/12
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
相关资源
最近更新 更多