content_image/856997.jpg
<img src='content_image/856997.jpg'>

partition_clause  has this syntax: 

partition_clause : PARTITION BY expr  [, expr ] ... 

## Note 

Partitioning for window functions differs from table partitioning. For information about table partitioning, see Chapter 24, Partitioning . 

Standard SQL requires PARTITION BY  to be followed by column names only. A MySQL extension is to permit expressions, not just column names. For example, if a table contains a TIMESTAMP  column named ts , standard SQL permits PARTITION BY ts  but not PARTITION BY HOUR(ts) , whereas MySQL permits both. 

• order_clause : An ORDER BY  clause indicates how to sort rows in each partition. Partition rows that are equal according to the ORDER BY  clause are considered peers. If ORDER BY  is omitted, partition rows are unordered, with no processing order implied, and all partition rows are peers. 

order_clause  has this syntax: 

order_clause : ORDER BY expr  [ASC|DESC] [, expr  [ASC|DESC]] ... 

Each ORDER BY  expression optionally can be followed by ASC  or DESC  to indicate sort direction. The default is ASC  if no direction is specified. NULL  values sort first for ascending sorts, last for descending sorts. 

An ORDER BY  in a window definition applies within individual partitions. To sort the result set as a whole, include an ORDER BY  at the query top level. 

• frame_clause : A frame is a subset of the current partition and the frame clause specifies how to define the subset. The frame clause has many subclauses of its own. For details, see Section 12.21.3, “Window Function Frame Specification” . 

## 12.21.3 Window Function Frame Specification 

The definition of a window used with a window function can include a frame clause. A frame is a subset of the current partition and the frame clause specifies how to define the subset. 

Frames are determined with respect to the current row, which enables a frame to move within a partition depending on the location of the current row within its partition. Examples: 

•By defining a frame to be all rows from the partition start to the current row, you can compute running totals for each row. 

•By defining a frame as extending N  rows on either side of the current row, you can compute rolling averages. 

The following query demonstrates the use of moving frames to compute running totals within each group of time-ordered level  values, as well as rolling averages computed from the current row and the rows that immediately precede and follow it: 

mysql> SELECT time, subject, val, SUM(val) OVER (PARTITION BY subject ORDER BY time ROWS UNBOUNDED PRECEDING) AS running_total, 

