好的,我花了一些时间在这方面进行研究,并找到了适用于我的解决方案。我还有一些工作要做,以引起复利兴趣。
CREATE PROCEDURE [dbo].[personalfinanceTest]
@Input_AgeCurrent INT
,@Input_AgeRetire INT
,@Input_Income DECIMAL(10,2)
,@Input_IncomeIncreasePct DECIMAL(10,6)
[email protected] decimal(10,2),
----Investment
,@Input_401k_PersonalPct DECIMAL(10,2)
,@Input_401k_EmployerPct DECIMAL(10,2)
,@Input_IRA_TradAmt DECIMAL(10,2)
,@Input_IRA_RothAmt DECIMAL(10,2)
----Market
[email protected] decimal(10,2),
[email protected] decimal(10,2)
AS
CREATE TABLE #Table
([Age] INT NOT NULL
,[Years] INT NULL
,[Income] DECIMAL(10,2) NULL
,[401k Personal] DECIMAL(10,2) NULL
,[IRA Traditional] DECIMAL(10,2) NULL
,[Taxable Income] DECIMAL(10,2) NULL
,[Tax] DECIMAL(10,2) NULL
,[PostTax Income] DECIMAL(10,2) NULL
,[Savings] DECIMAL(10,2) NULL
,[401k Employer] DECIMAL(10,2) NULL
,[401k Total] DECIMAL(10,2) NULL
,[IRA Roth] DECIMAL(10,2) NULL
,[IRA Total] DECIMAL(10,2) NULL
,[Cumulative Savings] DECIMAL(10,2) NULL
,[Cumulative Retirement] DECIMAL(10,2) NULL
)
DECLARE @age INT = @Input_AgeCurrent
DECLARE @prev_savings DECIMAL(10,2) = 0
DECLARE @prev_retirement DECIMAL(10,2) = 0
WHILE @age <= @Input_AgeRetire
BEGIN
DECLARE @years INT = @age - @Input_AgeCurrent
DECLARE @income DECIMAL(10,2) = @Input_Income * POWER(@Input_IncomeIncreasePct + 1,@years)
DECLARE @401k_personal DECIMAL(10,2) = @Input_401k_PersonalPct * @income
DECLARE @401k_employer DECIMAL(10,2) = @Input_401k_EmployerPct * @income
DECLARE @tax DECIMAL(10,2) = dbo.calculateTax(@income - @401k_personal)
DECLARE @IRA_trad DECIMAL(10,2) = @Input_IRA_TradAmt
DECLARE @IRA_roth DECIMAL(10,2) = @Input_IRA_RothAmt
DECLARE @income_taxable DECIMAL(10,2) = @income - @401k_personal
DECLARE @income_posttax DECIMAL(10,2) = @income - @tax
DECLARE @401kTotal DECIMAL(10,2) = @401k_personal + @401k_employer
DECLARE @IRATotal DECIMAL(10,2) = @IRA_trad + @IRA_roth
DECLARE @retirement DECIMAL(10,2) = @401kTotal + @IRATotal
DECLARE @savings DECIMAL(10,2) = @income_posttax - @retirement
INSERT INTO #Table([Age]
,[Years]
,[Income]
,[401k Personal]
,[IRA Traditional]
,[Taxable Income]
,[Tax]
,[PostTax Income]
,[Savings]
,[401k Employer]
,[401k Total]
,[IRA Roth]
,[IRA Total]
,[Cumulative Savings]
,[Cumulative Retirement])
SELECT @age
,@years
,@income
,@401k_personal
,@IRA_trad
,@income_taxable
,@tax
,@income_posttax
,@savings
,@401k_employer
,@401kTotal
,@IRA_roth
,@IRATotal
,@prev_savings + @savings
,@prev_retirement + @retirement
SET @age += 1
SET @prev_savings += @savings
SET @prev_retirement += @retirement
END
SELECT *
FROM #Table
此外,我创建了一个功能,2016年的税收等级:
CREATE FUNCTION [dbo].[calculateTax] (@salary AS money)
RETURNS money
AS
BEGIN
DECLARE @tax money
DECLARE @10high money = 9275
,@15low money = 9276
,@15high money = 37650
,@25low money = 37651
,@25high money = 91150
,@28low money = 91151
,@28high money = 190150
,@33low money = 190151
,@33high money = 413350
,@35low money = 413351
IF @salary < @10high SET @tax = @salary * 0.1
ELSE IF @salary < @15high SET @tax = @10high * 0.1 + (@salary - @15low)*0.15
ELSE IF @salary < @25high SET @tax = @10high * 0.1 + (@15high - @15low)*0.15 + (@salary - @25low)*0.25
ELSE SET @tax = @10high * 0.1 + (@15high - @15low)*0.15 + (@25high - @25low)*0.25 + (@salary - @35low)*0.35
RETURN @tax
END;
所有+1首先确定使用存储过程。你在正确的轨道上。这是我学习SQLServer的方式,除了我使用了体育统计数据。恕我直言,SQL Server中最重要的一件事就是SQL语言以及根据集合而不是行来思考的能力。一旦你掌握了它,其他一切都会变得更简单。但对于你的情况,我建议你选择你想要做的一些共同的财务计算,并开始玩它并在SQL Server中实现它。 – objectNotFound
大多数想要实现的内容都可以使用Select语句来完成,除非情况很复杂并且需要除select以外的结构和功能。我建议你先尝试理解连接的概念并实现一些基本的场景。有很多可用的帮助。祝你好运。 – objectNotFound
嗨,谢谢你的反馈。我主要尝试创建“年份和年龄”列,然后在选择语句中执行财务计算。所以它看起来像这样:SELECT Years,Age,@Income * 1.05^[Years] AS Savings FROM [YearsAgeTable]。我想[YearsAgeTable]是动态的,所以它始于Age变量。 – Walker