根据条件sql将值添加到列

时间:2018-07-13 12:42:57

标签: tsql

我正在使用过程填充Excel工作表,但是在正确的列中填充正确的值时遇到了困难。

SQL代码

DepreciationAmount=case when companyassets.RenewedDate is null 
then Cast(dbo.fn_v_Percent(DepreciationRules.EMPCDR_DepriciationRulePercentage,AcquiredValue) as numeric(20,2)) 
when CompanyAssets.RenewedDate is not null  then
else  Cast(dbo.fn_v_Percent(DepreciationRules.EMPCDR_DepriciationRulePercentage,AcquiredValue) as numeric(20,2)) 
-  Cast(dbo.fn_v_Percent(DepreciationRules2.EMPCDR_DepriciationRulePercentage,cast(dbo.fn_v_DepreciationCalculationValue(AcquiredDate,DateAdd(yy,1,GetDate()), AcquiredValue, DepreciationRules.EMPCDR_DepriciationRuleYears)as numeric(20,2))) as numeric(20,2)) end,

上面的代码随后被分配给变量

,_9_Year=CASE WHEN dbo.fn_v_isDepreciation(F9Label,DatePurchased,DepreciationRuleYears) = 1  THEN DepreciationAmount ELSE 0 END
    ,_8_Year=CASE WHEN dbo.fn_v_isDepreciation(F8Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END
    ,_7_Year=CASE WHEN dbo.fn_v_isDepreciation(F7Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END
    ,_6_Year=CASE WHEN dbo.fn_v_isDepreciation(F6Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END
    ,_5_Year=CASE WHEN dbo.fn_v_isDepreciation(F5Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END
    ,_4_Year=CASE WHEN dbo.fn_v_isDepreciation(F4Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END
    ,_3_Year=CASE WHEN dbo.fn_v_isDepreciation(F3Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END
    ,_2_Year=CASE WHEN dbo.fn_v_isDepreciation(F2Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount  ELSE 0 END
    ,_1_Year=CASE WHEN dbo.fn_v_isDepreciation(F1Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount  ELSE 0 END
    ,_0_Year=CASE WHEN dbo.fn_v_isDepreciation(F0Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END
    ,_00_Year=CASE WHEN dbo.fn_v_isDepreciation(F11Label,DatePurchased,DepreciationRuleYears) = 1 THEN DepreciationAmount ELSE 0 END

Excel工作表看起来像这张Excel工作表

Excel sheet

我面临的问题是,我希望有一个条件,如果续订日期不为null,则特定年份(即前5年)(如excel表格所示)的x金额为剩余的年数为y。

谢谢

0 个答案:

没有答案