2 列的移动平均值

发布于 2024-09-30 07:02:56 字数 574 浏览 2 评论 0原文

你好,我有一个问题。我知道如何使用 Oracle 分析函数计算最近 3 个月的移动平均值...但我的情况有点不同

Month-----ProductType-----Sales----------Average(HAVE查找此)

1---------A-------------10

1---------B-------- -------12

1---------C----------------17

2---------A---- -----------21

3---------C----------------2

3---------B ---------------21

4---------B----------------23

5

6

7

8

9

所以我们有每个月和每种产品类型的销售额...我需要计算过去 3 个月和特定产品的移动平均值。

例如:

对于第 4 个月和产品 B,它将是 (21+0+12)/3

有什么想法吗?

Hello I have a problem. I know how to calculate moving average last 3 months using oracle analytic functions... but my situatiion is a little different

Month-----ProductType-----Sales----------Average(HAVE TO FIND THIS)

1---------A---------------10

1---------B---------------12

1---------C---------------17

2---------A---------------21

3---------C---------------2

3---------B---------------21

4---------B---------------23

5

6

7

8

9

So we have sales for each month and each product type... I need to calculate the moving average of the last 3 months and the particular product.

example:

For month 4 and Produt B it would be (21+0+12)/3

Any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2024-10-07 07:02:56

另一种选择是使用分析函数的窗口子句

with my_data as (
  select 1 as month, 'A' as product, 10 as sales from dual union all
  select 1 as month, 'B' as product, 12 as sales from dual union all
  select 1 as month, 'C' as product, 17 as sales from dual union all
  select 2 as month, 'A' as product, 21 as sales from dual union all
  select 3 as month, 'C' as product, 2 as sales from dual union all
  select 3 as month, 'B' as product, 21 as sales from dual union all
  select 4 as month, 'B' as product, 23 as sales from dual 
)
select 
  month, 
  product, 
  sales,
  nvl(sum(sales) 
    over (partition by product order by month 
          range between 3 preceding and 1 preceding),0)/3 as average_sales
from my_data
order by month, product

Another option is to use the windowing clause of analytic functions

with my_data as (
  select 1 as month, 'A' as product, 10 as sales from dual union all
  select 1 as month, 'B' as product, 12 as sales from dual union all
  select 1 as month, 'C' as product, 17 as sales from dual union all
  select 2 as month, 'A' as product, 21 as sales from dual union all
  select 3 as month, 'C' as product, 2 as sales from dual union all
  select 3 as month, 'B' as product, 21 as sales from dual union all
  select 4 as month, 'B' as product, 23 as sales from dual 
)
select 
  month, 
  product, 
  sales,
  nvl(sum(sales) 
    over (partition by product order by month 
          range between 3 preceding and 1 preceding),0)/3 as average_sales
from my_data
order by month, product
醉梦枕江山 2024-10-07 07:02:56
SELECT month, 
       productType, 
       sales,
       (lag(sales, 3) over (partition by produtType order by month) +
        lag(sales, 2) over (partition by productType order by month) + 
        lag(sales, 1) over (partition by productType order by month))/3 moving_avg
  FROM your_table_name

-- 编辑以添加缺少的右括号

SELECT month, 
       productType, 
       sales,
       (lag(sales, 3) over (partition by produtType order by month) +
        lag(sales, 2) over (partition by productType order by month) + 
        lag(sales, 1) over (partition by productType order by month))/3 moving_avg
  FROM your_table_name

-- edited to add missing right paren

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文