The table obs records one observation per row: an integer value for a group group_name, which is 'A', 'B' or 'C'. Run a Kruskal-Wallis test across the groups in a single query.
Pool all the values and rank them from smallest (rank 1) to largest, giving tied values the average (mid) rank of the positions they occupy. Then compute
H=N(N+1)12i∑niRi2−3(N+1)
where Ri is the rank sum of group i, ni is the number of rows in group i, and N is the total number of rows. Compare H against the chi-square critical value stored in critical_values for the test's degrees of freedom at α=0.05.
Input Table(s)
obs
| Column | Type | Meaning |
|---|
| group_name | VARCHAR(5) | Group label, one of 'A', 'B', 'C' |
| value | INT | The observed value |
No primary key. Two rows with the same group and value are two separate observations.
critical_values
| Column | Type | Meaning |
|---|
| df | INT | Degrees of freedom |
| alpha | DECIMAL(4,3) | Significance level |
| critical_value | DECIMAL(8,4) | Chi-square critical value for that df and alpha |
Primary key: (df, alpha).
Output Contract
Return exactly one row with these columns, in this order:
| Column | Meaning |
|---|
| h_statistic | H rounded to 4 decimal places |
| df | Number of groups present in obs, minus 1 |
| critical_value | The critical_value for that df at alpha = 0.05, as stored |
| decision | 'reject' when H is strictly greater than critical_value, otherwise 'accept' |
Make the comparison with the unrounded H. The result is a single row, so no ordering is needed.
Example
obs
| group_name | value |
|---|
| C | 1 |
| A | 16 |
| B | 5 |
| A | 17 |
| B | 10 |
| C | 13 |
| A | 5 |
| B | 11 |
| C | 4 |
| B | 6 |
| A | 13 |
| C | 12 |
critical_values
| df | alpha | critical_value |
|---|
| 2 | 0.05 | 5.991 |
| 2 | 0.01 | 9.21 |
| 3 | 0.05 | 7.815 |
| 3 | 0.01 | 11.345 |
Expected output:
| h_statistic | df | critical_value | decision |
|---|
| 2.8942 | 2 | 5.991 | accept |
The two 5s sit at positions 3 and 4, so both get rank 3.5. The two 13s sit at positions 9 and 10, so both get rank 9.5. Three groups give 2 degrees of freedom, and 2.8942 is below 5.991.
Constraints and Clarifications
-
Ranks come from the pooled values of all groups together, so a tie between two groups gives both rows the same average rank.
-
Use the formula exactly as written. Do not divide by a tie-correction factor.
-
df
counts the groups that have at least one row in
obs
. At least two groups are present.
-
group_name
and
value
are never NULL.
-
critical_values
always has a row for the needed
df
at
alpha = 0.05
. Rows for other
alpha
levels must not affect the result.
-
'accept'
means the test fails to reject the null hypothesis that all groups come from the same distribution.