Logo

RANK_AVG

Updated 2023-06-15 22:14:55.007000

Syntax

SELECT * FROM [westclintech].[wct].[RANK_AVG](
   <@x_y_Query, nvarchar(max),>)

Description

Use the table-valued function RANK_AVG to calculate the ranks for a collection of x- and y-values. Tied ranks receive the average of ranks for the tied value. Ranks are calculated in ascending order.

Arguments

@x_y_Query

a T-SQL statement, as a string, that specifies the x- and y-values. The x- and y-values must be of the type float or of a type that implicitly converts to float.

Return Type

table

{"columns": [{"field": "colName", "headerName": "Name", "header": "name"}, {"field": "colDatatype", "headerName": "Type", "header": "type"}, {"field": "colDesc", "headerName": "Description", "header": "description", "minWidth": 1000}], "rows": [{"id": "d111009b-b2a4-4f36-9a12-95fafc89e8c2", "colName": "x", "colDatatype": "float", "colDesc": "the supplied x-value"}, {"id": "9d5aa749-156b-4a11-9bea-f98f07f5bebb", "colName": "y", "colDatatype": "float", "colDesc": "the supplied y-value"}, {"id": "cd5f9090-8431-4a7c-85b4-e1ecabd0ffca", "colName": "xrank", "colDatatype": "float", "colDesc": "the average rank of the x-value in the dataset of x-values"}, {"id": "c1241c91-acd0-4cf6-bb07-28453db9c75c", "colName": "yrank", "colDatatype": "float", "colDesc": "the average rank of the y-value in the dataset of y-values"}]}

Remarks

The function is insensitive to order; it does not matter what order the x- and y-values are passed in.

The x- and y-values pairs will be returned along with the average ranks.

Examples

SELECT *
INTO #r
FROM (
SELECT 125,110 UNION ALL
SELECT 115,122 UNION ALL
SELECT 130,125 UNION ALL
SELECT 140,120 UNION ALL
SELECT 140,140 UNION ALL
SELECT 115,124 UNION ALL
SELECT 140,123 UNION ALL
SELECT 125,137 UNION ALL
SELECT 140,135 UNION ALL
SELECT 135,145
) n(x,y);
 
SELECT *
FROM wct.RANK_AVG('SELECT x,y from #r')
DROP TABLE #r;

This produces the following result.

{"columns":[{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"xrank","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"yrank","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"x":"125","y":"110","xrank":"3.5","yrank":"1"},{"x":"115","y":"122","xrank":"1.5","yrank":"3"},{"x":"130","y":"125","xrank":"5","yrank":"6"},{"x":"140","y":"120","xrank":"8.5","yrank":"2"},{"x":"140","y":"140","xrank":"8.5","yrank":"9"},{"x":"115","y":"124","xrank":"1.5","yrank":"5"},{"x":"140","y":"123","xrank":"8.5","yrank":"4"},{"x":"125","y":"137","xrank":"3.5","yrank":"8"},{"x":"140","y":"135","xrank":"8.5","yrank":"7"},{"x":"135","y":"145","xrank":"6","yrank":"10"}]}

We could also have passed the same data into the function with the following syntax.

SELECT *
FROM wct.RANK_AVG('SELECT 125,110 UNION ALL
SELECT 115,122 UNION ALL
SELECT 130,125 UNION ALL
SELECT 140,120 UNION ALL
SELECT 140,140 UNION ALL
SELECT 115,124 UNION ALL
SELECT 140,123 UNION ALL
SELECT 125,137 UNION ALL
SELECT 140,135 UNION ALL
SELECT 135,145 ');

This produces the following result.

{"columns":[{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"xrank","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"yrank","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"x":"125","y":"110","xrank":"3.5","yrank":"1"},{"x":"115","y":"122","xrank":"1.5","yrank":"3"},{"x":"130","y":"125","xrank":"5","yrank":"6"},{"x":"140","y":"120","xrank":"8.5","yrank":"2"},{"x":"140","y":"140","xrank":"8.5","yrank":"9"},{"x":"115","y":"124","xrank":"1.5","yrank":"5"},{"x":"140","y":"123","xrank":"8.5","yrank":"4"},{"x":"125","y":"137","xrank":"3.5","yrank":"8"},{"x":"140","y":"135","xrank":"8.5","yrank":"7"},{"x":"135","y":"145","xrank":"6","yrank":"10"}]}

Since this is a table-valued function, you can re-arrange the output using TSQL. In the following example, we will put the data in x-value order and we will move the xrank column next to the x column.

SELECT x,xrank,y,yrank
FROM wct.RANK_AVG('SELECT 125,110 UNION ALL
SELECT 115,122 UNION ALL
SELECT 130,125 UNION ALL
SELECT 140,120 UNION ALL
SELECT 140,140 UNION ALL
SELECT 115,124 UNION ALL
SELECT 140,123 UNION ALL
SELECT 125,137 UNION ALL
SELECT 140,135 UNION ALL
SELECT 135,145 ')
ORDER BY 1, 3;

This produces the following result.

{"columns":[{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"xrank","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"yrank","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"x":"115","xrank":"1.5","y":"122","yrank":"3"},{"x":"115","xrank":"1.5","y":"124","yrank":"5"},{"x":"125","xrank":"3.5","y":"110","yrank":"1"},{"x":"125","xrank":"3.5","y":"137","yrank":"8"},{"x":"130","xrank":"5","y":"125","yrank":"6"},{"x":"135","xrank":"6","y":"145","yrank":"10"},{"x":"140","xrank":"8.5","y":"120","yrank":"2"},{"x":"140","xrank":"8.5","y":"123","yrank":"4"},{"x":"140","xrank":"8.5","y":"135","yrank":"7"},{"x":"140","xrank":"8.5","y":"140","yrank":"9"}]}