【问题标题】:Calculate Average Cost for last 7 or less days in SQL在 SQL 中计算过去 7 天或更少天的平均成本
【发布时间】:2019-05-02 23:13:12
【问题描述】:

我有一个如下所示的 SQL Server 2012 表:

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 的值:

  1. 如果有过去 7 天或更少的行,则取这些天的“成本”的简单平均值
  2. 如果过去 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 天前有行时才有效

(例如:如果我有从 2019 年 4 月 1 日到 2019 年 4 月 7 日的行,我可以得到 2019 年 4 月 8 日的正确值)

预期结果:

 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

【问题讨论】:

  • 嘿,这只是一个仅供参考,对于我们这些美国以外的人来说,以美国格式查看日期实际上真的很混乱。如果您使用与位置无关的日期格式(例如“20190104”),您将对回答者产生更广泛的“吸引力”。

标签: sql-server variables dateadd


【解决方案1】:

所以我最终重写了你的查询以对我有意义,并添加了实际代码来获取你的示例。我还添加了代码以获取平均值缺失或小于01。在那里的某个地方我做了一些事情,使它完全按照你指定的方式工作,但我不知道在哪里抱歉。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2022-06-28
    相关资源
    最近更新 更多