Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - SQL 的用法 » 查询和修改数据 » OLAP 支持 » SQL Anywhere 中的窗口函数 » 基本集合函数

 

AVG 函数示例

在此示例中,AVG 用作窗口函数,来按月计算 2000 年所有产品销售额的移动平均值。请注意,WINDOW 说明使用 RANGE 子句,这会导致根据月值计算窗口边界,而不是像 ROWS 子句那样按相邻行的数目来计算边界。例如,如果部分或全部产品在某个特定的月完全没有销售额,则使用 ROWS 将产生不同的结果。

SELECT *
  FROM ( SELECT s.ProductID, 
         Month( o.OrderDate ) AS julian_month,
         SUM( s.Quantity ) AS sales,
         AVG( SUM( s.Quantity ) )
         OVER ( PARTITION BY s.ProductID
           ORDER BY Month( o.OrderDate ) ASC
           RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING ) 
         AS average_sales
         FROM SalesOrderItems s KEY JOIN SalesOrders o
         WHERE Year( o.OrderDate ) = 2000
         GROUP BY s.ProductID, Month( o.OrderDate ) ) 
  AS DT
  ORDER BY 1,2;
另请参见