AntDB-Oracle Compatibility Developer’s Manual P3–52
Published: 2023-09-13 11:41:44

##### KEEP function description

keep is a special analysis function, his use is different from the analysis function specified by the over keyword, can be used for such an occasion: take the same group to a field sorted, the specified field to take the smallest or largest value of that.

**Syntax:**

min \| max(column1) keep (dense\_rank first \| last order by column2)

\[ over (partion by column3) ]

**Example:**

```
SELECT deptno, MIN(t.mgr)  KEEP (DENSE_RANK FIRST ORDER BY t.sal) a   
from emp t  group by deptno;
```

Meaning: grouped by deptno, sorted by sal within the group, DENSE\_RANK FIRST means to keep the sal ranked in front of a group of data (when the sal ranked in front has duplicate values, save multiple data). Then in each group of records, execute the previous aggregation function, here is min(t.mgr).

Execution results:

```
 DEPTNO |  A   
--------+------
     10 | 7782
     30 | 7698
     20 | 7902
(3 rows)
```

Aggregation functions with statistical use

| Name| Number of arguments| Function| For example:
|----------|----------|----------|----------
| COVAR\_POP| 2| COVAR\_POP(expr1, expr2) returns the overall covariance of a pair of expressions| 
| COVAR\_SAMP| 2| COVAR\_SAMP(expr1, expr2) returns the sample covariance of a pair of expressions| 
| CUME\_DIST| 0~n| Calculate the cumulative distribution of a value in a set of values| CUME\_DIST(15500, .05) WITHIN GROUP (ORDER BY salary, commission\_pct)
| RANK| 0~n| Determines the ranking of a set of values based on the ORDER BY expression in the OVER clause. When there are the same sorted values, there will be the same ranking and the number of rows with the same values will be recorded in the next ranking.| RANK(15500) WITHIN GROUP (ORDER BY salary DESC)
| DENSE\_RANK| 0~n| Calculates the sorted values in a set of values, sorted consecutively, with two second places still followed by the third place| DENSE\_RANK() OVER (PARTITION BY department\_id ORDER BY salary)
| PERCENT\_RANK| 0~n| Percentage ranking| PERCENT\_RANK(15000, .05) WITHIN GROUP (ORDER BY salary
| REGR\_SLOPE| 2| REGR\_SLOPE: return slope, equal to COVAR\_POP(expr1, expr2) / VAR\_POP(expr2)| REGR\_SLOPE(expr1, expr2)
| REGR\_INTERCEPT| 2| REGR\_INTERCEPT: returns the y-intercept of the regression line, equal to| REGR\_INTERCEPT(expr1, expr2)
| REGR\_COUNT| 2| Returns the number of non-empty pairs of numbers used to fill the regression line| REGR\_COUNT(expr1, expr2)
| REGR\_R2| 2| Return the coefficient of determination of the regression line, calculated as If VAR\_POP(expr2) = 0 then return NULL If VAR\_POP(expr1) = 0 and VAR\_POP(expr2) != 0 then return 1 If VAR\_POP(expr1) > 0 and VAR\_POP(expr2 != 0 then  return POWER(CORR(expr1,expr),2)| REGR\_R2(expr1, expr2)
| REGR\_AVGX| 2| Calculating the mean of the independent variable (expr2) of the regression line, after removing the null pair (expr1, expr2), is equal to AVG(expr2)| REGR\_AVGX(expr1, expr2)
| REGR\_AVGY| 2| Calculate the mean of the strain variable (expr1) of the regression line, after removing the null pair (expr1, expr2), which is equal to AVG(expr1)| REGR\_AVGY(expr1, expr2)
| REGR\_SXX| 2| The return value is equal to REGR\_COUNT(expr1, expr2) \* VAR\_POP(expr2)| REGR\_SXX(expr1, expr2)
| REGR\_SYY| 2| The return value is equal to REGR\_COUNT(expr1, expr2) \* VAR\_POP(expr1)| REGR\_SYY(expr1, expr2)
| REGR\_SXY| 2| The return value is equal to REGR\_COUNT(expr1, expr2) \* COVAR\_POP(expr1, expr2)| REGR\_SXY(expr1, expr2)
| STDDEV| 1| Calculate the standard deviation of the current row about the group| stddev(col) stddev(distinct col) stddev(all col)
| STDDEV\_POP| 1| This function calculates the overall standard deviation and returns the square root of the overall variable| STDDEV\_POP(salary)
| STDDEV\_SAMP| 1| This function calculates the sample standard deviation and returns the square root of the overall variable| STDDEV\_SAMP(salary)
| VAR\_POP| 1| This function returns the overall variable of the non-empty set (ignoring null)| VAR\_POP(salary)
| VAR\_SAMP| 1| This function returns the sample variable of the non-empty set (ignoring null)| VAR\_SAMP(salary)
| VARIANCE| 1| Returns 0 if the number of rows in the expression is 1, or VAR\_SAMP if the number of rows in the expression is greater than 1| variance(col)
| CORR| 2| Returns the correlation coefficient of a pair of expressions| CORR(list\_price, min\_price)