The syntax of the analytic function:

    Function

    Support Functions: AVG(), COUNT(), DENSE_RANK(), FIRST_VALUE(), LAG(), LAST_VALUE(), LEAD(), MAX(), MIN(), RANK(), ROW_NUMBER(), SUM()

    PARTITION BY clause

    ORDER BY clause

    The Order By clause is basically the same as the outer Order By. It defines the order in which the input rows are sorted, and if Partition By is specified, Order By defines the order within each Partition grouping. The only difference from the outer Order By is that the Order By n (n is a positive integer) in the OVER clause is equivalent to doing nothing, while the outer Order By n means sorting according to the nth column.

    Example:

    1. row_number() OVER (ORDER BY date_and_time) AS id,
    2. c1, c2, c3, c4
    3. FROM events;

    Window clause

    The Window clause is used to specify an operation range for the analytical function, the current row is the criterion, and several rows before and after are used as the object of the analytical function operation. The methods supported by the Window clause are: AVG(), COUNT(), FIRST_VALUE(), LAST_VALUE() and SUM(). For MAX() and MIN(), the window clause can specify the starting range UNBOUNDED PRECEDING

    syntax:

    1. create table stock_ticker (stock_symbol string, closing_price decimal(8,2), closing_date timestamp);
    2. select * from stock_ticker order by stock_symbol, closing_date
    3. | stock_symbol | closing_price | closing_date |
    4. | JDR | 12.86 | 2014-10-02 00:00:00 |
    5. | JDR | 12.94 | 2014-10-04 00:00:00 |
    6. | JDR | 12.55 | 2014-10-05 00:00:00 |
    7. | JDR | 14.03 | 2014-10-06 00:00:00 |
    8. | JDR | 14.75 | 2014-10-07 00:00:00 |

    This query uses the analytic function to generate the column moving_average, whose value is the 3-day average price of the stock, that is, the three-day average price of the previous day, the current day, and the next day. The first day has no value for the previous day, and the last day does not have the value for the next day, so these two lines only calculate the average of the two days. Partition By does not play a role here, because all the data are JDR data, but if there is other stock information, Partition By will ensure that the analysis function value acts within this Partition.