2017-11-14 41 views
0

以下は私のテーブル構造です。私は、各プロパティのリースの長さのために家賃を計算する必要があります。年を1年以上計算するSQL Server 2008R2

はのは、PropertyID = 12077を見てみましょう:

  • エリア= 1280
  • 開始日= 2023年2月1日
  • 終了日= 2027- 10-31
  • BaseRent = 21.53
  • RentIncreasePercent = 0.04(4%)
  • IncreaseRepeatMonths = 12ヶ月(注:最初の12か月再増税はありません)

この不動産のリースは、2023年から2028年の間に開始され、終了するため、毎年別々の行の賃貸料を知りたいと思います。これは、12ヶ月ごとに増加する(複合賃料の増加)を考慮に入れます。

例:
21.53 * 1280は、最初の12か月間は家賃を支払うでしょう。ただし、2月にリースが開始されたため、2023年の賃料総額は((21.23 * 1280)/ 12)* 11

2024年の最初の月の賃料は、 12ヶ月ごとに増加する。 (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は化合物の増加に由来する。

これを行うにはどうすればいいですか?私にとって最も混乱する部分は、1月に開始しないリース開始日の対応方法を知らないことです。


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 
+0

純粋なSQL(T-SQLなし)でそれを行うには、あなたの意図があるSQL 2008で働くことを願っていますか? – SmartDumb

+1

私はあなたがする必要があると思うのは、暦年と「リース」年で月のセットにあなたの日付範囲を爆発させることです。したがって、開始日は月2、暦年2023、賃貸年0です。次の行の月3、暦年2023、賃貸年号0です。行12は月1(暦年)、暦年2024、賃貸年鑑0、行13は月2、cal yr 2024、lease yr 1 次に、カレンダーを分割するために必要なデータと、賃料増加の乗数として使用される「リース」月が必要です。 – Beth

+0

@SmartDumb私は別のストアドプロシージャロジックにプラグインするために、ビュー内でそれを必要とします。しかし、パフォーマンスがあれば、t-SQLも同様に動作します。一日の終わりに、データはかなり複雑なssrsレポートで使用されます。 – NonProgrammer

答えて

2

私はあなたのテーブルからすべての月をcontaintsカレンダーテーブルを使用することをお勧めします。 私は私の例は

-- 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] 
+0

これは完全に機能しました!私はYearRentAmount式を更新するだけでした。あなたが持っているのは複合料金を計算することではありません。 – NonProgrammer

+0

次の式は次のとおりです。(BaseRent * Area *(Power(キャスト(1 + RentIncreasePercent as float)、IncreaseCount))))/ 12 – NonProgrammer