github.com/mithrandie/csvq@v1.18.1/docs/_posts/2006-01-02-analytic-functions.md (about)

     1  ---
     2  layout: default
     3  title: Analytic Functions - Reference Manual - csvq
     4  category: reference
     5  ---
     6  
     7  # Analytic Functions
     8  
     9  Analytic functions calculate values of groups.
    10  Analytic Functions can be used only in [Select Clause]({{ '/reference/select-query.html#select_clause' | relative_url }}) and [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
    11  
    12  | name | description |
    13  | :- | :- |
    14  | [ROW_NUMBER](#row_number)     | Return sequential numbers |
    15  | [RANK](#rank)                 | Return ranks |
    16  | [DENSE_RANK](#dense_rank)     | Return ranks without any gaps in the ranking |
    17  | [CUME_DIST](#cume_dist)       | Return cumulative distributions |
    18  | [PERCENT_RANK](#percent_rank) | Return relative ranks |
    19  | [NTILE](#ntile)               | Return the number of groups |
    20  | [FIRST_VALUE](#first_value)   | Return the first value in a group |
    21  | [LAST_VALUE](#last_value)     | Return the last value in a group |
    22  | [NTH_VALUE](#nth_value)       | Return the n-th value in a group |
    23  | [LAG](#lag)                   | Return the value in a previous row |
    24  | [LEAD](#lead)                 | Return the value in a following row |
    25  | [COUNT](#count)               | Return the number of values in a group |
    26  | [MIN](#min)                   | Return the minimum value in a group |
    27  | [MAX](#max)                   | Return the maximum value in a group |
    28  | [SUM](#sum)                   | Return the sum of values in a group |
    29  | [AVG](#avg)                   | Return the average of values in a group |
    30  | [STDEV](#stdev)               | Return the sample standard deviation of values |
    31  | [STDEVP](#stdevp)             | Return the population standard deviation of values |
    32  | [VAR](#var)                   | Return the sample variance of values |
    33  | [VARP](#varp)                 | Return the population variance of values |
    34  | [MEDIAN](#median)             | Return the median of values in a group |
    35  | [LISTAGG](#listagg)           | Return the concatenated string of values in a group |
    36  | [JSON_AGG](#json_agg)         | Return the string formatted in JSON array of values in a group |
    37  
    38  ## Basic Syntax
    39  {: #syntax}
    40  
    41  ```sql
    42  analytic_function
    43    : function_name([args]) OVER ([partition_clause] [order_by_clause [windowing_clause]])
    44  
    45  args
    46    : value [, value ...]
    47  
    48  partition_clause
    49    : PARTITION BY value [, value ...]
    50  
    51  windowing_clause
    52    : ROWS window_position
    53    | ROWS BETWEEN window_frame_low AND window_frame_high
    54  
    55  window_position
    56    : {UNBOUNDED PRECEDING|offset PRECEDING|CURRENT ROW}
    57  
    58  window_frame_low
    59    : {UNBOUNDED PRECEDING|offset PRECEDING|offset FOLLOWING|CURRENT ROW}
    60  
    61  window_frame_high
    62    : {UNBOUNDED FOLLOWING|offset PRECEDING|offset FOLLOWING|CURRENT ROW}
    63  
    64  ```
    65  
    66  _value_
    67  : [value]({{ '/reference/value.html' | relative_url }})
    68  
    69  _order_by_clause_
    70  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
    71  
    72  _offset_
    73  : [integer]({{ '/reference/value.html#integer' | relative_url }})
    74  
    75  Analytic Functions sort the result set by _order_by_clause_ and calculate values within each of groups partitioned by _partition_clause_.
    76  If there is no _partition_clause_, then all records of the result set are dealt with as one group. 
    77  
    78  
    79  ## Definitions
    80  
    81  ### ROW_NUMBER
    82  {: #row_number}
    83  
    84  ```
    85  ROW_NUMBER() OVER ([partition_clause] [order_by_clause])
    86  ```
    87  
    88  _partition_clause_
    89  : [Partition Clause](#syntax)
    90  
    91  _order_by_clause_
    92  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
    93  
    94  _return_
    95  : [integer]({{ '/reference/value.html#integer' | relative_url }})
    96  
    97  Returns the sequential numbers of records in a group.
    98  
    99  
   100  ### RANK
   101  {: #rank}
   102  
   103  ```
   104  RANK() OVER ([partition_clause] [order_by_clause])
   105  ```
   106  
   107  _partition_clause_
   108  : [Partition Clause](#syntax)
   109  
   110  _order_by_clause_
   111  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   112  
   113  _return_
   114  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   115  
   116  Returns the ranks of records in a group.
   117  
   118  
   119  ### DENSE_RANK
   120  {: #dense_rank}
   121  
   122  ```
   123  DENSE_RANK() OVER ([partition_clause] [order_by_clause])
   124  ```
   125  
   126  _partition_clause_
   127  : [Partition Clause](#syntax)
   128  
   129  _order_by_clause_
   130  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   131  
   132  _return_
   133  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   134  
   135  Returns the ranks of records without any gaps in the ranking in a group.
   136  
   137  
   138  ### CUME_DIST
   139  {: #cume_dist}
   140  
   141  ```
   142  CUME_DIST() OVER ([partition_clause] [order_by_clause])
   143  ```
   144  
   145  _partition_clause_
   146  : [Partition Clause](#syntax)
   147  
   148  _order_by_clause_
   149  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   150  
   151  _return_
   152  : [float]({{ '/reference/value.html#float' | relative_url }})
   153  
   154  Returns the cumulative distributions in a group.
   155  The return value is greater than 0 and less than or equal to 1.
   156  
   157  
   158  ### PERCENT_RANK
   159  {: #percent_rank}
   160  
   161  ```
   162  PERCENT_RANK() OVER ([partition_clause] [order_by_clause])
   163  ```
   164  
   165  _partition_clause_
   166  : [Partition Clause](#syntax)
   167  
   168  _order_by_clause_
   169  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   170  
   171  _return_
   172  : [float]({{ '/reference/value.html#float' | relative_url }})
   173  
   174  Returns the relative ranks in a group.
   175  The return value is greater than or equal to 0 and less than or equal to 1.
   176  
   177  
   178  ### NTILE
   179  {: #ntile}
   180  
   181  ```
   182  NTILE(number_of_groups) OVER ([partition_clause] [order_by_clause])
   183  ```
   184  
   185  _number_of_groups_
   186  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   187  
   188  _partition_clause_
   189  : [Partition Clause](#syntax)
   190  
   191  _order_by_clause_
   192  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   193  
   194  _return_
   195  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   196  
   197  Splits the records into _number_of_groups_, then returns the sequential numbers of the groups.
   198  
   199  
   200  ### FIRST_VALUE
   201  {: #first_value}
   202  
   203  ```
   204  FIRST_VALUE(expr) [IGNORE NULLS] OVER ([partition_clause] [order_by_clause [windowing_clause]])
   205  ```
   206  
   207  _expr_
   208  : [value]({{ '/reference/value.html' | relative_url }})
   209  
   210  _partition_clause_
   211  : [Partition Clause](#syntax)
   212  
   213  _order_by_clause_
   214  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   215  
   216  _return_
   217  : [primitive type]({{ '/reference/value.html#primitive_types' | relative_url }})
   218  
   219  Returns the first value in a group.
   220  If _IGNORE NULLS_ keywords are specified, then returns the first value that is not a null.
   221  
   222  
   223  ### LAST_VALUE
   224  {: #last_value}
   225  
   226  ```
   227  LAST_VALUE(expr) [IGNORE NULLS] OVER ([partition_clause] [order_by_clause [windowing_clause]])
   228  ```
   229  
   230  _expr_
   231  : [value]({{ '/reference/value.html' | relative_url }})
   232  
   233  _partition_clause_
   234  : [Partition Clause](#syntax)
   235  
   236  _order_by_clause_
   237  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   238  
   239  _return_
   240  : [primitive type]({{ '/reference/value.html#primitive_types' | relative_url }})
   241  
   242  Returns the last value in a group.
   243  If _IGNORE NULLS_ keywords are specified, then returns the last value that is not a null.
   244  
   245  
   246  ### NTH_VALUE
   247  {: #nth_value}
   248  
   249  ```
   250  NTH_VALUE(expr, n) [IGNORE NULLS] OVER ([partition_clause] [order_by_clause [windowing_clause]])
   251  ```
   252  
   253  _expr_
   254  : [value]({{ '/reference/value.html' | relative_url }})
   255  
   256  _n_
   257  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   258  
   259  _partition_clause_
   260  : [Partition Clause](#syntax)
   261  
   262  _order_by_clause_
   263  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   264  
   265  _return_
   266  : [primitive type]({{ '/reference/value.html#primitive_types' | relative_url }})
   267  
   268  Returns the _n_-th value in a group.
   269  If _IGNORE NULLS_ keywords are specified, then returns the _n_-th value excluding null values.
   270  
   271  
   272  ### LAG
   273  {: #lag}
   274  
   275  ```
   276  LAG(expr [, offset [, default]]) [IGNORE NULLS] OVER ([partition_clause] [order by clause])
   277  ```
   278  
   279  _expr_
   280  : [value]({{ '/reference/value.html' | relative_url }})
   281  
   282  _offset_
   283  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   284    
   285    The number of rows from current row. The default is 1.
   286  
   287  _default_
   288  : [value]({{ '/reference/value.html' | relative_url }})
   289  
   290    The value to set when the offset row does not exist.
   291    The default is NULL.
   292  
   293  _partition_clause_
   294  : [Partition Clause](#syntax)
   295  
   296  _order_by_clause_
   297  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   298  
   299  _return_
   300  : [primitive type]({{ '/reference/value.html#primitive_types' | relative_url }})
   301  
   302  Returns the value in a previous row.
   303  If _IGNORE NULLS_ keywords are specified, then rows that _expr_ values are null will be skipped. 
   304  
   305  
   306  ### LEAD
   307  {: #lead}
   308  
   309  ```
   310  LEAD(expr [, offset [, default]]) [IGNORE NULLS] OVER ([partition_clause] [order by clause])
   311  ```
   312  
   313  _expr_
   314  : [value]({{ '/reference/value.html' | relative_url }})
   315  
   316  _offset_
   317  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   318    
   319    The number of rows from current row. The default is 1.
   320  
   321  _default_
   322  : [value]({{ '/reference/value.html' | relative_url }})
   323  
   324    The value to set when the offset row does not exist.
   325    The default is NULL.
   326  
   327  _partition_clause_
   328  : [Partition Clause](#syntax)
   329  
   330  _order_by_clause_
   331  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   332  
   333  _return_
   334  : [primitive type]({{ '/reference/value.html#primitive_types' | relative_url }})
   335  
   336  Returns the value in a following row.
   337  If _IGNORE NULLS_ keywords are specified, then rows that _expr_ values are null will be skipped. 
   338  
   339  
   340  ### COUNT
   341  {: #count}
   342  
   343  ```
   344  COUNT([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   345  ```
   346  
   347  _expr_
   348  : [value]({{ '/reference/value.html' | relative_url }})
   349  
   350  _partition_clause_
   351  : [Partition Clause](#syntax)
   352  
   353  _return_
   354  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   355  
   356  Returns the number of non-null values of _expr_.
   357  
   358  ```
   359  COUNT([DISTINCT] *) OVER ([partition_clause])
   360  ```
   361  
   362  _partition_clause_
   363  : [Partition Clause](#syntax)
   364  
   365  _return_
   366  : [integer]({{ '/reference/value.html#integer' | relative_url }})
   367  
   368  Returns the number of all values including null values.
   369  
   370  
   371  ### MIN
   372  {: #min}
   373  
   374  ```
   375  MIN(expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   376  ```
   377  
   378  _expr_
   379  : [value]({{ '/reference/value.html' | relative_url }})
   380  
   381  _partition_clause_
   382  : [Partition Clause](#syntax)
   383  
   384  _return_
   385  : [primitive type]({{ '/reference/value.html#primitive_types' | relative_url }})
   386  
   387  Returns the minimum value of non-null values of _expr_.
   388  If all values are null, then returns a null.
   389  
   390  
   391  ### MAX
   392  {: #max}
   393  
   394  ```
   395  MAX(expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   396  ```
   397  
   398  _expr_
   399  : [value]({{ '/reference/value.html' | relative_url }})
   400  
   401  _partition_clause_
   402  : [Partition Clause](#syntax)
   403  
   404  _return_
   405  : [primitive type]({{ '/reference/value.html#primitive_types' | relative_url }})
   406  
   407  Returns the maximum value of non-null values of _expr_.
   408  If all values are null, then returns a null.
   409  
   410  
   411  ### SUM
   412  {: #sum}
   413  
   414  ```
   415  SUM([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   416  ```
   417  
   418  _expr_
   419  : [value]({{ '/reference/value.html' | relative_url }})
   420  
   421  _partition_clause_
   422  : [Partition Clause](#syntax)
   423  
   424  _return_
   425  : [float]({{ '/reference/value.html#float' | relative_url }})
   426  
   427  Returns the sum of float values of _expr_.
   428  If all values are null, then returns a null.
   429  
   430  
   431  ### AVG
   432  {: #avg}
   433  
   434  ```
   435  AVG([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   436  ```
   437  
   438  _expr_
   439  : [value]({{ '/reference/value.html' | relative_url }})
   440  
   441  _partition_clause_
   442  : [Partition Clause](#syntax)
   443  
   444  _return_
   445  : [float]({{ '/reference/value.html#float' | relative_url }})
   446  
   447  Returns the average of float values of _expr_.
   448  If all values are null, then returns a null.
   449  
   450  
   451  ### STDEV
   452  {: #stdev}
   453  
   454  ```
   455  STDEV([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   456  ```
   457  
   458  _expr_
   459  : [value]({{ '/reference/value.html' | relative_url }})
   460  
   461  _partition_clause_
   462  : [Partition Clause](#syntax)
   463  
   464  _return_
   465  : [float]({{ '/reference/value.html#float' | relative_url }})
   466  
   467  Returns the sample standard deviation of float values of _expr_.
   468  If all values are null, then returns a null.
   469  
   470  
   471  ### STDEVP
   472  {: #stdevp}
   473  
   474  ```
   475  STDEVP([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   476  ```
   477  
   478  _expr_
   479  : [value]({{ '/reference/value.html' | relative_url }})
   480  
   481  _partition_clause_
   482  : [Partition Clause](#syntax)
   483  
   484  _return_
   485  : [float]({{ '/reference/value.html#float' | relative_url }})
   486  
   487  Returns the population standard deviation of float values of _expr_.
   488  If all values are null, then returns a null.
   489  
   490  
   491  ### VAR
   492  {: #var}
   493  
   494  ```
   495  VAR([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   496  ```
   497  
   498  _expr_
   499  : [value]({{ '/reference/value.html' | relative_url }})
   500  
   501  _partition_clause_
   502  : [Partition Clause](#syntax)
   503  
   504  _return_
   505  : [float]({{ '/reference/value.html#float' | relative_url }})
   506  
   507  Returns the sample variance of float values of _expr_.
   508  If all values are null, then returns a null.
   509  
   510  
   511  ### VARP
   512  {: #varp}
   513  
   514  ```
   515  VARP([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   516  ```
   517  
   518  _expr_
   519  : [value]({{ '/reference/value.html' | relative_url }})
   520  
   521  _partition_clause_
   522  : [Partition Clause](#syntax)
   523  
   524  _return_
   525  : [float]({{ '/reference/value.html#float' | relative_url }})
   526  
   527  Returns the population variance of float values of _expr_.
   528  If all values are null, then returns a null.
   529  
   530  
   531  ### MEDIAN
   532  {: #median}
   533  
   534  ```
   535  MEDIAN([DISTINCT] expr) OVER ([partition_clause] [order_by_clause [windowing_clause]])
   536  ```
   537  
   538  _expr_
   539  : [value]({{ '/reference/value.html' | relative_url }})
   540  
   541  _partition_clause_
   542  : [Partition Clause](#syntax)
   543  
   544  _return_
   545  : [float]({{ '/reference/value.html#float' | relative_url }})
   546  
   547  Returns the median of float or datetime values of _expr_.
   548  If all values are null, then returns a null.
   549  
   550  Even if _expr_ values are datetime values, this function returns a float or integer value.
   551  The return value can be converted to a datetime value by using the [DATETIME function]({{ '/reference/cast-functions.html#datetime' | relative_url }}).
   552  
   553  
   554  ### LISTAGG
   555  {: #listagg}
   556  
   557  ```
   558  LISTAGG([DISTINCT] expr [, separator]) OVER ([partition_clause] [order by clause])
   559  ```
   560  
   561  _expr_
   562  : [value]({{ '/reference/value.html' | relative_url }})
   563  
   564  _separator_
   565  : [string]({{ '/reference/value.html#string' | relative_url }})
   566  
   567  _partition_clause_
   568  : [Partition Clause](#syntax)
   569  
   570  _order_by_clause_
   571  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   572  
   573  _return_
   574  : [string]({{ '/reference/value.html#string' | relative_url }})
   575  
   576  Returns the string result with the concatenated non-null values of _expr_.
   577  If all values are null, then returns a null.
   578  
   579  _separator_ is placed between values. Empty string is the default.
   580  
   581  
   582  
   583  ### JSON_AGG
   584  {: #json_agg}
   585  
   586  ```
   587  JSON_AGG([DISTINCT] expr) OVER ([partition_clause] [order by clause])
   588  ```
   589  
   590  _expr_
   591  : [value]({{ '/reference/value.html' | relative_url }})
   592  
   593  _partition_clause_
   594  : [Partition Clause](#syntax)
   595  
   596  _order_by_clause_
   597  : [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})
   598  
   599  _return_
   600  : [string]({{ '/reference/value.html#string' | relative_url }})
   601  
   602  Returns the string formatted in JSON array of _expr_.