计算 SQL 最近 7 天或更短时间内的平均成本
Calculate Average Cost for last 7 or less days in SQL
我有一个 SQL Server 2012 table 看起来像这样:
Date Product Cost AvgCost
----------------------------------
4/7/2019 ProdA 3 NULL
4/9/2019 ProdA 2 NULL
4/10/2019 ProdA 4 NULL
4/24/2019 ProdA 4 NULL
4/30/2019 ProdA 1 NULL
我正在尝试根据以下条件计算 AvgCost 的值:
- 如果有最近 7 天或更短时间的行,则取这些天 "Cost" 的简单平均值
- 如果过去 7 天或更短时间内不存在任何行,则只需为 AvgCost 输入 1
代码:
SELECT [Date], Product, Cost, oa.AvgCost
FROM [Table1] A
OUTER APPLY
(SELECT AVG(A1.Cost) as AvgCost
FROM [Table1] A1
WHERE A1.Product = A.Product
AND A1.date BETWEEN DATEADD (Day, -6, DATEADD(DAY, -1, A.date))
AND DATEADD(DAY, -1, A.date)) oa
WHERE
a.date BETWEEN '04/1/2019' AND '04/30/2019'
该代码似乎只有在正好有 7 天之前的行时才有效
(例如:如果我有日期为 4/1/2019 - 4/7/2019 的行,我能够获得 4/8/2019 的正确值)
预期结果:
Date Product Cost AvgCost
4/7/2019 ProdA 3 1 -- no rows exist for last 7 days or
-- less then 1
4/9/2019 ProdA 2 3 -- Only 1 rows exists for last 7 days or
-- less then average for that day
4/10/2019 ProdA 4 2.5 -- Average cost for 4/7 and 4/9
4/24/2019 ProdA 4 1 -- no rows exist for last 7 days or
-- less then 1
4/30/2019 ProdA 1 4 --Only 1 rows exists for last 7 days or
-- less then average for that day
实际结果
Date Product Cost AvgCost
4/7/2019 ProdA 3 NULL
4/9/2019 ProdA 2 NULL
4/10/2019 ProdA 4 NULL
4/24/2019 ProdA 4 NULL
4/30/2019 ProdA 1 NULL
因此,我重写了一堆您的查询以使对我有意义,并添加了实际代码以获取您的示例。我还添加了代码以获取平均值缺失或小于 0
的 1
。我在那里的某个地方做了一些事情,使它完全按照你指定的方式工作,但我不知道在哪里抱歉。
drop table if exists #table1
create table #table1 (d date, Product nvarchar(max), Cost float, AvgCost FLOAT)
insert into #table1 values ('20190407', 'ProdA', 3, null)
insert into #table1 values ('20190409', 'ProdA', 2, null)
insert into #table1 values ('20190410', 'ProdA', 4, null)
insert into #table1 values ('20190424', 'ProdA', 4, null)
insert into #table1 values ('20190430', 'ProdA', 1, null)
SELECT [d], Product, Cost, iif(isnull(oa.AvgCost, 0) < 1, 1, oa.AvgCost) as AvgCost
FROM [#table1] A
OUTER APPLY
(
SELECT AVG(A1.Cost) as AvgCost
FROM [#table1] as A1
WHERE A1.Product = A.Product
AND A1.d BETWEEN DATEADD(Day, -7, A.d)
AND DATEADD(DAY, -1, A.d)
) as oa
WHERE a.d BETWEEN '20190104' AND '20190430'
结果:
d Product Cost AvgCost
2019-04-07 ProdA 3 1
2019-04-09 ProdA 2 3
2019-04-10 ProdA 4 2.5
2019-04-24 ProdA 4 1
2019-04-30 ProdA 1 4
我有一个 SQL Server 2012 table 看起来像这样:
Date Product Cost AvgCost
----------------------------------
4/7/2019 ProdA 3 NULL
4/9/2019 ProdA 2 NULL
4/10/2019 ProdA 4 NULL
4/24/2019 ProdA 4 NULL
4/30/2019 ProdA 1 NULL
我正在尝试根据以下条件计算 AvgCost 的值:
- 如果有最近 7 天或更短时间的行,则取这些天 "Cost" 的简单平均值
- 如果过去 7 天或更短时间内不存在任何行,则只需为 AvgCost 输入 1
代码:
SELECT [Date], Product, Cost, oa.AvgCost
FROM [Table1] A
OUTER APPLY
(SELECT AVG(A1.Cost) as AvgCost
FROM [Table1] A1
WHERE A1.Product = A.Product
AND A1.date BETWEEN DATEADD (Day, -6, DATEADD(DAY, -1, A.date))
AND DATEADD(DAY, -1, A.date)) oa
WHERE
a.date BETWEEN '04/1/2019' AND '04/30/2019'
该代码似乎只有在正好有 7 天之前的行时才有效
(例如:如果我有日期为 4/1/2019 - 4/7/2019 的行,我能够获得 4/8/2019 的正确值)
预期结果:
Date Product Cost AvgCost
4/7/2019 ProdA 3 1 -- no rows exist for last 7 days or
-- less then 1
4/9/2019 ProdA 2 3 -- Only 1 rows exists for last 7 days or
-- less then average for that day
4/10/2019 ProdA 4 2.5 -- Average cost for 4/7 and 4/9
4/24/2019 ProdA 4 1 -- no rows exist for last 7 days or
-- less then 1
4/30/2019 ProdA 1 4 --Only 1 rows exists for last 7 days or
-- less then average for that day
实际结果
Date Product Cost AvgCost
4/7/2019 ProdA 3 NULL
4/9/2019 ProdA 2 NULL
4/10/2019 ProdA 4 NULL
4/24/2019 ProdA 4 NULL
4/30/2019 ProdA 1 NULL
因此,我重写了一堆您的查询以使对我有意义,并添加了实际代码以获取您的示例。我还添加了代码以获取平均值缺失或小于 0
的 1
。我在那里的某个地方做了一些事情,使它完全按照你指定的方式工作,但我不知道在哪里抱歉。
drop table if exists #table1
create table #table1 (d date, Product nvarchar(max), Cost float, AvgCost FLOAT)
insert into #table1 values ('20190407', 'ProdA', 3, null)
insert into #table1 values ('20190409', 'ProdA', 2, null)
insert into #table1 values ('20190410', 'ProdA', 4, null)
insert into #table1 values ('20190424', 'ProdA', 4, null)
insert into #table1 values ('20190430', 'ProdA', 1, null)
SELECT [d], Product, Cost, iif(isnull(oa.AvgCost, 0) < 1, 1, oa.AvgCost) as AvgCost
FROM [#table1] A
OUTER APPLY
(
SELECT AVG(A1.Cost) as AvgCost
FROM [#table1] as A1
WHERE A1.Product = A.Product
AND A1.d BETWEEN DATEADD(Day, -7, A.d)
AND DATEADD(DAY, -1, A.d)
) as oa
WHERE a.d BETWEEN '20190104' AND '20190430'
结果:
d Product Cost AvgCost
2019-04-07 ProdA 3 1
2019-04-09 ProdA 2 3
2019-04-10 ProdA 4 2.5
2019-04-24 ProdA 4 1
2019-04-30 ProdA 1 4