Create Calculated Column based on another table wi

2019-08-21 17:01发布

I need to create a calculated column that looks at the Field called Forecast Multiplier and if it is 1 use the number in field fp.month1 then if it is 1.5 use fp.month1 and 1/2 of fp.month2 and so on up to 12 months. So I figured I would create a function and then base the calculated column on the function. Here is the function:

BEGIN
    DECLARE @MinInventory as int;
    DECLARE @MyDate DATETIME = GETDATE()
    DECLARE @WeekNumber as int = DATEDIFF(WEEK, DATEADD(MONTH, DATEDIFF(MONTH, 0, @MyDate), 0), @MyDate) + 1
    DECLARE @MONTH1 as int = convert(int, [dbo].[ForecastPivot].MONTH1)

    SELECT @MinInventory =
        CASE 
        when @ForecastMultiplier = 1 Then fp.month1
        when @ForecastMultiplier = 1.5 Then fp.month1 + (fp.month2 * .5)
        when @ForecastMultiplier = 2 Then fp.month1 + fp.month2
        when @ForecastMultiplier = 2.5 Then fp.month2 + fp.month2 + (fp.month3 * .5)
        when @ForecastMultiplier = 3 Then fp.month1 + fp.month2 + fp.month3
        when @ForecastMultiplier = 3.5 Then fp.month1 + fp.month2 + fp.month3 + (fp.month4 * .5)
        when @ForecastMultiplier = 4 Then fp.month1 + fp.month2 + fp.month3 + fp.month4
        when @ForecastMultiplier = 4.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + (fp.month5 * .5)
        when @ForecastMultiplier = 5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5
        when @ForecastMultiplier = 5.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + (fp.month6 * .5)
        when @ForecastMultiplier = 6 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6
        when @ForecastMultiplier = 6.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + (fp.month7 * .5)
        when @ForecastMultiplier = 7 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7
        when @ForecastMultiplier = 7.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + (fp.month8 * .5)
        when @ForecastMultiplier = 8 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8
        when @ForecastMultiplier = 8.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + (fp.month9 * .5)
        when @ForecastMultiplier = 9 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + fp.month9
        when @ForecastMultiplier = 9.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + fp.month9 + (fp.month10 * .5)
        when @ForecastMultiplier = 10 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + fp.month9 + fp.month10
        when @ForecastMultiplier = 10.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + fp.month9 + fp.month10 + (fp.month11 * .5)
        when @ForecastMultiplier = 11 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + fp.month9 + fp.month10 + fp.month11
        when @ForecastMultiplier = 11.5 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + fp.month9 + fp.month10 + fp.month11 + (fp.month12 * .5)
        when @ForecastMultiplier = 12 Then fp.month1 + fp.month2 + fp.month3 + fp.month4 + fp.month5 + fp.month6 + fp.month7 + fp.month8 + fp.month9 + fp.month10 + fp.month11 + fp.month12
        end
    FROM [dbo].[MinimumProductInfoes] mpi
    join [dbo].[ForecastPivot] fp on fp.EQCODE = @EquivCode AND fp.FWHSE = @region
    RETURN Round(@MinInventory, 0)
END

The function works correctly but it is very slow. It takes about 10 seconds to run it once. The table I am going to use this on has a few hundred rows, so this would take a few minutes to execute; not acceptable. How can I speed this up? If I cannot I am thinking about running a job overnight to do this.

0条回答
登录 后发表回答