我需要创建一个计算列来查看名为Forecast Multiplier的字段,如果它是1则使用字段fp.month1中的数字然后如果它是1.5则使用fp.month1和fp.month2的1/2等等长达12个月。所以我想我会创建一个函数,然后将计算列基于函数。这是功能:
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
该功能正常但速度很慢。运行一次大约需要10秒钟。我打算使用它的表有几百行,所以这需要几分钟才能执行;不能接受的。我怎样才能加快速度呢?如果我不能,我想在一夜之间找到一份工作来做这件事。