The obs table records a continuous value for two groups (group_name is 'A' or 'B'). Rank every value across the pooled sample from lowest to highest with RANK(). Return one row per group with group_name, rank_sum (the sum of its members' ranks), and u_statistic, rounded to 1 decimal:
U=R−2n(n+1)
where R is the group's rank_sum and n is the group's size.
Input Table(s)
obs
| Column | Type | Meaning |
|---|
| group_name | VARCHAR(5) | Group label, 'A' or 'B' |
| value | DECIMAL(8,2) | The observed value |
No primary key. Two rows with the same group and value are two separate observations.
Output Contract
Return one row per group with these columns, in this order:
| Column | Meaning |
|---|
| group_name | The group label |
| rank_sum | Sum of the pooled ranks of the group's rows |
| u_statistic | U for the group, rounded to 1 decimal place |
Order the rows by group_name ascending.
Example
obs
| group_name | value |
|---|
| A | 45.29 |
| B | 38.23 |
| B | 95.29 |
| A | 84.41 |
| A | 89.11 |
| A | 69.88 |
| B | 19.34 |
| B | 31.81 |
| A | 64.17 |
| B | 48.91 |
| A | 61.55 |
| B | 48.56 |
Expected output:
| group_name | rank_sum | u_statistic |
|---|
| A | 49 | 28.0 |
| B | 29 | 8.0 |
B holds pooled positions 1, 2, 3, 5, 6 and 12, so its rank sum is 29 and its U is 29 - 21 = 8. A holds the other six positions, which sum to 49.
Constraints and Clarifications
-
Ranks come from all rows of both groups together.
-
Use
RANK()
as stated: tied values share the lowest position of their block, with no averaging. The values are continuous, so ties are rare.
-
group_name
and
value
are never NULL, and each group has at least one row.
-
Duplicate rows all count, both in the ranking and in
n
.