本季度的平均销售额与上一季度的平均销售额

avg sale of quarter with previous quarter avg sale

我有一个 table 一个,其中有各种属性,如区域产品、年份、季度、月份、销售。我必须计算具有相同区域的每个产品的 avg_qtr 销售额,并显示他们之前的 avg_qtr sale.I 已经阅读过关于滞后的信息,但在这里无法使用,因为它在如何之后不固定许多行它将被重复。我的table结构是这样的

   Region Product Year Qtr Month Sales 

    NORTH   P1    2015  1   JAN 1000
    NORTH   P1    2015  1   FEB 2000
    NORTH   P1    2015  1   MAR 3000
    NORTH   P1    2015  2   APR 4000
    NORTH   P1    2015  2   MAY 5000
    NORTH   P1    2015  2   JUN 6000
    NORTH   P1    2015  3   JUL 7000
    NORTH   P1    2015  3   AUG 8000
    NORTH   P1    2015  3   SEP 9000
    NORTH   P1    2015  4   OCT 1000
    NORTH   P1    2015  4   DEC 4000
    NORTH   P1    2015  4   NOV 2000
    NORTH   P3    2015  1   FEB 1000
    NORTH   P3    2015  1   FEB 9000
    NORTH   P3    2015  2   APR 2000
    NORTH   P3    2015  3   JUL 8000
    NORTH   P1    2016  1   MAR 3000
    NORTH   P1    2016  1   FEB 1000
    NORTH   P1    2016  1   JAN 2000
    SOUTH   P1    2015  1   JAN 2000
    SOUTH   P1    2015  1   FEB 3000
    SOUTH   P1    2015  1   JAN 4000
    SOUTH   P2    2015  1   MAR 1000
    SOUTH   P2    2015  1   JAN 8000
    SOUTH   P2    2015  1   FEB 9000
    SOUTH   P2    2015  2   JUN 9000
    SOUTH   P2    2015  2   MAY 8000
    SOUTH   P2    2015  2   APR 2000
    SOUTH   P2    2015  3   SEP 4000
    SOUTH   P2    2015  3   AUG 2000
    SOUTH   P2    2015  3   JUL 1000
    SOUTH   P2    2015  4   NOV 2000
    SOUTH   P2    2015  4   DEC 1000
    SOUTH   P2    2015  4   OCT 5000
    SOUTH   P3    2015  3   AUG 9000
    SOUTH   P3    2015  4   OCT 1000
    SOUTH   P3    2015  4   NOV 3000
    SOUTH   P2    2016  1   JAN 2000
    SOUTH   P2    2016  1   JAN 4000

我编写了计算当前 qtr 的查询,并显示前一季度与当前一季度的平均值

  WITH AvgSales
AS (SELECT
region,
product,
year,
qtr,
ROUND(AVG(sales), 2) AS avg_Sale
FROM one 
GROUP BY region,
product,
year,qtr
 )
SELECT
s.region,
s.product,
s.year,
s.month,
s.sales,
avg.qtr,
avg.avg_Sale AS Qtr_Avg_Sale,
prev.avg_sale AS Prev_Qtr_Avg_Sale
FROM one s
JOIN AvgSales avg
ON s.region = avg.region
AND s.product = avg.product
AND s.QTR = avg.qtr
AND s.year = avg.year
LEFT JOIN AvgSales prev
ON  (s.region = prev.region
AND s.product = prev.product
AND s.year - 1 = prev.year
and s.qtr=1
AND prev.qtr = 4) or
(s.region = prev.region
AND s.product = prev.product
AND s.year = prev.year
AND s.qtr - 1 = prev.qtr) ;

我能够获得该产品的当前平均值和之前的平均值,但反之则不行。我不确定如何显示当前季度没有任何销售的该季度的先前平均值。 我想要这样的输出-

Region  Product  Year  qtr  month   sale  avg_Sale     prev_avg_sale
    NORTH     P1     2015   1   JAN     1000    2000    
    NORTH     P1     2015   1   FEB     2000    2000    
    NORTH     P1     2015   1   MAR     3000    2000    
    NORTH     P1     2015   2   APR     4000    5000            2000
    NORTH     P1     2015   2   MAY     5000    5000            2000
    NORTH     P1     2015   2   JUN     6000    5000            2000
    NORTH     P1     2015   3   JUL     7000    8000            5000
    NORTH     P1     2015   3   AUG     8000    8000            5000
    NORTH     P1     2015   3   SEP     9000    8000            5000
    NORTH     P1     2015   4   OCT     1000    2333.33         8000
    NORTH     P1     2015   4   NOV     2000    2333.33         8000
    NORTH     P1     2015   4   DEC     4000    2333.33         8000
    SOUTH     P2     2015   1   JAN     8000    6000    
    SOUTH     P2     2015   1   FEB     9000    6000    
    SOUTH     P2     2015   1   MAR     1000    6000    
    SOUTH     P2     2015   2   APR     2000    6333.33         6000
    SOUTH     P2     2015   2   MAY     8000    6333.33         6000
    SOUTH     P2     2015   2   JUN     9000    6333.33         6000
    SOUTH     P2     2015   3   JUL     1000    2333.33       6333.33
    SOUTH     P2     2015   3   AUG     2000    2333.33       6333.33
    SOUTH     P2     2015   3   SEP     4000    2333.33       6333.33
    SOUTH     P2     2015   4   OCT     5000    2666.67       2333.33
    SOUTH     P2     2015   4   NOV     2000    2666.67       2333.33
    SOUTH     P2     2015   4   DEC     1000    2666.67       2333.33
    NORTH     P3     2015   1   FEB     9000    5000    
    NORTH     P3     2015   1   FEB     1000    5000    
    NORTH     P3     2015   2   APR     2000    2000           5000
    NORTH     P3     2015   3   JUL     8000    8000           2000
    SOUTH     P3     2015   3   AUG     9000    9000    
    SOUTH     P3     2015   4   OCT     1000    2000           9000
    SOUTH     P3     2015   4   NOV     3000    2000           9000
    NORTH     P1     2016   1   JAN     2000    2000         2333.33
    NORTH     P1     2016   1   FEB     1000    2000         2333.33
    NORTH     P1     2016   1   MAR     3000    2000         2333.33
    NORTH     P2     2016   2                   2000
    SOUTH     P2     2016   1   JAN     2000    3000         2666.67
    SOUTH     P2     2016   1   JAN     4000    3000         2666.67
    SOUTH     P2     2016   2                   3000  
    SOUTH     P1     2015   1   JAN     4000    3000    
    SOUTH     P1     2015   1   JAN     2000    3000    
    SOUTH     P1     2015   1   FEB     3000    3000        

您需要加入一些子 select 语句,这些语句将获得上一季度的平均值。您还需要执行两个语句的并集,因为对于第 2、3、4 季度,您可以简单地将连接语句中的四分之一减去之前的 qtr avg,但是当它是第一季度时,您需要减去一年并设置前一个 qtr = 4。此语句应该适用于您所描述的内容。

--handles when the current quarter being viewed is 2,3,or 4 because those would still be in the same year when looking at the previous quarter
select  t1.product,
        t1.year,
        t1.month,
        t1.sales ,
        t1.qtr,
        round(avg(t1.sales) over (partition by t1.qtr,t1.year),2) as av,
        t2.prev_av
from one t1
left join ( select
                product,
                year,
                month,
                sales ,
                qtr,
                round(avg(sales) over (partition by qtr,year),2) as prev_av
            from one
            ) t2
on t1.year = t2.year
and (t1.qtr - 1) = t2.qtr
where t1.qtr in (2,3,4)
union
--handles the 1st quarter of the year when you need to grab the 4th quarter of the previous year for the previous avg
select  t3.product,
        t3.year,
        t3.month,
        t3.sales ,
        t3.qtr,
        round(avg(t3.sales) over (partition by t3.qtr,t3.year),2) as av,
        t4.prev_av
from one t3
left join ( select
                product,
                year,
                month,
                sales,
                qtr,
                round(avg(sales) over (partition by qtr,year),2) as prev_av
            from one
            ) t4
on (t3.year - 1) = t4.year
and t4.qtr = 4
where t3.qtr = 1;

如果您有一个排序值作为排序依据,您可以使用分析函数的窗口子句,因此首先创建一个 DENSE_RANKing 年和季度,然后在您的分析函数中使用该排名:

with t1 as ( 
  select one.*
       , dense_rank() over (order by year, qtr) qord
    from one
)
select product
     , year
     , qtr
     , month
     , sales
     , round(avg(sales) over (partition by qord),2) qtr_avg
     , round(avg(sales) over (order by qord
                              range between 1 preceding
                                        and 1 preceding),2) prev_qtr_avg
  from t1

上述解决方案假设样本数据集中提供了密集的季度数据,但是,如果数据在季度维度上是稀疏的,您可以先按以下查询对数据进行加密:

with qtrs as (select level qtr from dual connect by level <=4)
, t1 as ( 
  select product
       , year
       , qtrs.qtr
       , month
       , sales
       , dense_rank() over (order by year, qtrs.qtr) qord
    from qtrs
    left outer join one partition by (year)
      on one.qtr = qtrs.qtr
)
select product
     , year
     , qtr
     , month
     , sales
     , round(avg(sales) over (partition by qord),2) qtr_avg
     , round(avg(sales) over (order by qord
                              range between 1 preceding
                                        and 1 preceding),2) prev_qtr_avg
  from t1

这确保了对于数据中表示的每一年,每个季度至少存在一行,因此 QORD 将每季度枚举一次,数据中的差距将导致计算出的季度平均值存在差距。

您也可以通过利用 YEAR 和 QTR 的数字性质来改变计算 QORD 的方式来达到类似的效果,如本例所示:

with t1 as (select one.*, year*4+qtr qord from one)
select product
     , year
     , qtr
     , month
     , sales
     , round(avg(sales) over (partition by qord),2) qtr_avg
     , round(avg(sales) over (order by qord
                              range between 1 preceding
                                        and 1 preceding),2) prev_qtr_avg
  from t1

这里不需要致密化,但它仍然正确地在 prev_qtr_avg 中留下了空白,但它确实遗漏了致密化数据包含的缺失季度的记录。

结合最后两个示例,并在您对区域的新要求中添加每季度至少返回或生成一行数据(如果每个不同的区域、产品和年份需要)。两个平均值均按地区和产品划分,并视情况按当前或上一季度计算:

with qtrs(qtr) as (select level from dual connect by level <= 4)
, t1 as (
select region, product, year, q.qtr, month, sales, year*4+q.qtr qord
  from qtrs q
  left join one partition by (region, product, year)
    on q.qtr = one.qtr
)
select region
     , product
     , year
     , qtr
     , month
     , sales
     , round(avg(sales) over (partition by region, product, qord),2) avg_sale
     , round(avg(sales) over (partition by region, product
                              order by qord
                              range between 1 preceding
                                        and 1 preceding),2) prev_avg_sale
  from t1
 order by year, region, qtr, product;

根据最新要求进行了编辑。

您的问题是您试图通过操纵 QTR 和 YEAR 来获取 previous_avg。我正在使用 RANK 函数,按照我希望的数据排名方式排序。在连接中,我确保平均区域 = 上一个区域并且忽略年份,因为上一季度可能是平均年份 Q1 的上一年的 Q4;这样更干净。

    --Build the test table
    IF OBJECT_ID('SALES','U') IS NOT NULL
        DROP TABLE SALES

    CREATE TABLE SALES
    (
          Region  VARCHAR(255)
        , Product VARCHAR(10)
        , [Year]  INT
        , QTR     INT
        , [Month] VARCHAR(19)
        , Sales   DECIMAL(19,4)
    );

    INSERT SALES
    VALUES
         ('NORTH', 'P1', 2015, 1, 'JAN', 1000)
        ,('NORTH', 'P1', 2015, 1, 'FEB', 2000)
        ,('NORTH', 'P1', 2015, 1, 'MAR', 3000)
        ,('NORTH', 'P1', 2015, 2, 'APR', 4000)
        ,('NORTH', 'P1', 2015, 2, 'MAY', 5000)
        ,('NORTH', 'P1', 2015, 2, 'JUN', 6000)
        ,('NORTH', 'P1', 2015, 3, 'JUL', 7000)
        ,('NORTH', 'P1', 2015, 3, 'AUG', 8000)
        ,('NORTH', 'P1', 2015, 3, 'SEP', 9000)
        ,('NORTH', 'P1', 2015, 4, 'OCT', 1000)
        ,('NORTH', 'P1', 2015, 4, 'DEC', 4000)
        ,('NORTH', 'P1', 2015, 4, 'NOV', 2000)
        ,('NORTH', 'P3', 2015, 1, 'FEB', 1000)
        ,('NORTH', 'P3', 2015, 1, 'FEB', 9000)
        ,('NORTH', 'P3', 2015, 2, 'APR', 2000)
        ,('NORTH', 'P3', 2015, 3, 'JUL', 8000)
        ,('NORTH', 'P1', 2016, 1, 'MAR', 3000)
        ,('NORTH', 'P1', 2016, 1, 'FEB', 1000)
        ,('NORTH', 'P1', 2016, 1, 'JAN', 2000)
        ,('SOUTH', 'P1', 2015, 1, 'JAN', 2000)
        ,('SOUTH', 'P1', 2015, 1, 'FEB', 3000)
        ,('SOUTH', 'P1', 2015, 1, 'JAN', 4000)
        ,('SOUTH', 'P2', 2015, 1, 'MAR', 1000)
        ,('SOUTH', 'P2', 2015, 1, 'JAN', 8000)
        ,('SOUTH', 'P2', 2015, 1, 'FEB', 9000)
        ,('SOUTH', 'P2', 2015, 2, 'JUN', 9000)
        ,('SOUTH', 'P2', 2015, 2, 'MAY', 8000)
        ,('SOUTH', 'P2', 2015, 2, 'APR', 2000)
        ,('SOUTH', 'P2', 2015, 3, 'SEP', 4000)
        ,('SOUTH', 'P2', 2015, 3, 'AUG', 2000)
        ,('SOUTH', 'P2', 2015, 3, 'JUL', 1000)
        ,('SOUTH', 'P2', 2015, 4, 'NOV', 2000)
        ,('SOUTH', 'P2', 2015, 4, 'DEC', 1000)
        ,('SOUTH', 'P2', 2015, 4, 'OCT', 5000)
        ,('SOUTH', 'P3', 2015, 3, 'AUG', 9000)
        ,('SOUTH', 'P3', 2015, 4, 'OCT', 1000)
        ,('SOUTH', 'P3', 2015, 4, 'NOV', 3000)
        ,('SOUTH', 'P2', 2016, 1, 'JAN', 2000)
        ,('SOUTH', 'P2', 2016, 1, 'JAN', 4000);


    --CTE TO CAPTURE AVG SALES BY REGION, PRODCUT, YEAR, QTR;  OMIT PRODUCT IF YOU WANT STRAIGHT UP QUARTER AVG, REGARDLESS OF PRODCUCT
    WITH cteAvgSales AS
    (
        SELECT Region, Product, [Year], QTR, AVG(Sales) current_avg
            , RANK() OVER(ORDER BY Region, Product, [Year], QTR) AS RNK
        FROM SALES
        GROUP BY Region, Product, [Year], QTR
    )
    SELECT s.Region, s.Product, s.[Year] AS [year], s.QTR AS [quarter], s.[Month], s.Sales, a.current_avg, p.current_avg AS previous_avg
    FROM SALES s
        INNER JOIN cteAvgSales a ON a.Region = s.Region
            AND a.Product = s.Product
            AND a.[Year]  = s.[Year]
            AND a.QTR = s.QTR
        LEFT JOIN cteAvgSales p ON p.Region = a.Region
            AND p.Product = s.Product
            AND p.RNK=a.RNK-1
    ORDER BY s.Region, s.Product, s.[Year], s.QTR