简体   繁体   English

SQL计算运行平均值

[英]SQL Calculate Running average

I have the below table which contains data for purchase quantity, price etc. I need to calculate the running average (current average) for each row. 我有下表,其中包含购买数量,价格等数据。我需要计算每行的移动平均值(当前平均值)。

The first currentAverage Value for receipt 1 is £75 because 500 units were purchased at £75. 收据1的第一个currentAverage Value为75英镑,因为以75英镑购买了500个单位。 there were no previous units so the first one is calculated with (PurchaseQty * IntakeSellingPrice) / IntakeSellingPrice = CurrentAvg 没有先前的单位,因此第一个单位的计算方式为(PurchaseQty * IntakeSellingPrice)/ IntakeSellingPrice = CurrentAvg

I have manually calculated receipt 2 to by "£79.4858" using the following method: 我使用以下方法手动计算了收据2的“ 79.4858”:

*select *, ((PurchaseQty * IntakeSellingPrice) + (InventoryBalance * )) / NewBalance [CurrentAVG]* *选择*,(((购买数量*摄入销售价格)+(库存余额*))/ NewBalance [CurrentAVG] *

CREATE TABLE [dbo].[X](
[Item No_] [nvarchar](20) NOT NULL,
[ReceiptNo] [bigint] NULL,
[Sold] [decimal](38, 20) NULL,
[InventoryBalance] [decimal](38, 20) NOT NULL,
[PurchaseQty] [decimal](38, 20) NULL,
[IntakeSellingPrice] [decimal](38, 20) NULL,
[NewBalance] [decimal](38, 20) NULL,
[CurrentAverage] [numeric](2, 2) NOT NULL
) ON [PRIMARY]
GO

INSERT [dbo].[X] ([Item No_], [ReceiptNo], [Sold], [InventoryBalance], [PurchaseQty], [IntakeSellingPrice], [NewBalance], [CurrentAverage]) VALUES (N'2000045', 1, CAST(0.00000000000000000000 AS Decimal(38, 20)), CAST(0.00000000000000000000 AS Decimal(38, 20)), CAST(500.00000000000000000000 AS Decimal(38, 20)), CAST(75.00000000000000000000 AS Decimal(38, 20)), CAST(500.00000000000000000000 AS Decimal(38, 20)), CAST(0.00 AS Numeric(2, 2))) GO
INSERT [dbo].[X] ([Item No_], [ReceiptNo], [Sold], [InventoryBalance], [PurchaseQty], [IntakeSellingPrice], [NewBalance], [CurrentAverage]) VALUES (N'2000045', 2, CAST(250.00000000000000000000 AS Decimal(38, 20)), CAST(250.00000000000000000000 AS Decimal(38, 20)), CAST(2181.00000000000000000000 AS Decimal(38, 20)), CAST(80.00000000000000000000 AS Decimal(38, 20)), CAST(2431.00000000000000000000 AS Decimal(38, 20)), CAST(0.00 AS Numeric(2, 2))) GO
INSERT [dbo].[X] ([Item No_], [ReceiptNo], [Sold], [InventoryBalance], [PurchaseQty], [IntakeSellingPrice], [NewBalance], [CurrentAverage]) VALUES (N'2000045', 3, CAST(316.00000000000000000000 AS Decimal(38, 20)), CAST(2115.00000000000000000000 AS Decimal(38, 20)), CAST(10.00000000000000000000 AS Decimal(38, 20)), CAST(80.00000000000000000000 AS Decimal(38, 20)), CAST(2125.00000000000000000000 AS Decimal(38, 20)), CAST(0.00 AS Numeric(2, 2)))

You can join each row with all other rows that come before it in the running average, and then find their average grouped by the former row. 您可以将每一行与运行平均值中排在其前面的所有其他行合并,然后找到按前一行分组的平均值。 So, assuming that no 2 rows are the same, this will work: 因此,假设没有两行相同,这将起作用:

SELECT a.*, AVG(b.CurrentAverage) 
FROM dbo.X a INNER JOIN dbo.X b
ON a.[Item No_] >= b.[Item No_] --or whatever you are ordering your records by for the running average
GROUP BY a.[Item No_], ..., a.[CurrentAverage]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM