将新计算列(上周的平均销售额4)添加到现有表中

时间:2015-04-06 14:16:29

标签: sql sql-server calculated-columns

我有桌子:

SSID distrProdName distrBranchID distrID salesQty weekID

1,产品,1,7,1,434,10

2,产品,2,7,1,223,10

3,产品,1,7,1,23,11

4,产品,2,7,1,23,11

5,产品,1,7,1,43,12

6,产品,2,7,1,12,12

7,产品,1,7,1,232,13

8,产品,2,7,1,47,13

我想添加一个新的计算列“AvSales4LastW”

在MS Excel中我解决了这样的问题:

SSID distrProdName distrBranchID distrID salesQty weekID AvSales4LastW代码code1 code2 code3

1;产品1; 7; 1; 434; 10; 108,5;产品17110;产品1719;产品1718;产品1717

2;产品2; 7; 1; 223; 10; 55,75;产品27110;产品2719;产品2718;产品2717;

3;产品1; 7; 1; 23; 11; 114,25;产品17111;产品17110;产品1719;产品1718;

4;产品2; 7; 1; 23; 11; 61.5;产品27111;产品27110;产品; 2719产品2718;

5;产品1; 7; 1; 43; 12; 125;产品17112;产品17111;产品17110;产品1719;

6;产品2; 7; 1; 12; 12; 64,5;产品27112;产品27111;产品27110;产品2719;

7;产品1; 7; 1; 232; 13; 183;产品17113;产品17112;产品17111;产品17110;

8;产品2; 7; 1; 47; 13; 76,25;产品27113;产品27112;产品27111;产品27110;

H9中的Folmula: = B9& C9& D9& F9

I9中的公式: = B9& C9& D9& F9-1

J9中的公式: = B9& C9& D9& F9-2

K9中的公式: = B9& C9& D9& F9-3

AvSales4LastW(G9)= AVERAGE(E9; SUMIF($ H:$ H; $ I9; $ E:$ E); SUMIF($ H:$ H; $ J9; $ E: $ E); SUMIF($ H:$ H; $ K9; $ E:$ E))=(47 + 12 + 23 + 223)/ 4 = 76,25

如何在MS SQL SERVER中添加新列(AvSales4LastW)? 对不起,我无法添加图片

请帮帮我

任何想法

由于

3 个答案:

答案 0 :(得分:2)

据我所知,您将无法在表中添加COMPUTED列,这会导致表中的前一行。但是,您可以使用分区上的平均值派生rolling average,按周数排序,如下所示:

SELECT *, avg(salesQty) over (partition by distrProdName order by weekID 
          ROWS between 3 preceding and current row) AS AvSales4LastW
FROM MyTable;

但请注意,这需要每周为每个产品连续/连续的行(无间隙)。

SqlFiddle here

答案 1 :(得分:2)

您可以添加引用COMPUTED COLUMN的{​​{1}}来获取结果。以下是此工作的示例模型:

测试数据:

FUNCTION

功能:

Create Table TestComputed
(
    PK int not null identity(1,1),
    ProductName Varchar (100),
    B int,
    C int,
    Qty int,
    Wk int
)
Go

Insert TestComputed (ProductName, B, C, Qty, Wk)
Values  ('1', 7, 1, 434, 10),
        ('2', 7, 1, 223, 10),
        ('1', 7, 1, 23, 11),
        ('2', 7, 1, 23, 11),
        ('1', 7, 1, 43, 12),
        ('2', 7, 1, 12, 12),
        ('1', 7, 1, 232, 13),
        ('2', 7, 1, 47, 13)

更改表格:

Create Function dbo.fnGet4WeekAverage(@WeekId Int, @ProductName Varchar (100))
Returns int
As Begin
    Declare @Average Int = 0

    Select  @Average = Avg(Qty)
    From    TestComputed
    Where   Wk Between @WeekId - 3 And @WeekId
    And     ProductName = @ProductName

    Return @Average
End
Go

结果:

Alter Table TestComputed Add AvSales4LastW As dbo.fnGet4WeekAverage(wk, ProductName)

答案 2 :(得分:0)

如果你有不同的ProdName,Branch,City,WeekID:

,你可以使用这个功能

功能:

Create Function dbo.fnGet4WeekAverage(@ProductName nvarchar (150), @BranchName int, @DistrID int, @weekID Int)
Returns float
As Begin
    Declare @Average float = 0

    Select  @Average = Avg([salesQty])
    From    [dbo].[weeklySalesStocks]
    Where   [weekID] Between @weekID - 3 And @weekID
    And     [distrProdName] = @ProductName
    And     [distrBranchID] = @BranchName
    And     [distrID] = @DistrID 

    Return @Average
End
Go

alter table:

Alter Table [dbo].[weeklySalesStocks] Add AvSales4LastW As dbo.fnGet4WeekAverage([distrProdName], [distrBranchID], [distrID], [weekID])