Table scores records a numeric value per subject, and each subject belongs to one group grp, which is 'A', 'B' or 'C'. Perform a one-way ANOVA:
SSB=∑gng(xˉg−xˉ)2,SSW=∑g∑i∈g(xi−xˉg)2,F=SSW/(N−k)SSB/(k−1)
Here ng and xˉg are the row count and mean value of group g, xˉ is the mean of value over all rows, k is the number of groups and N the total number of rows. Return one row with f_statistic rounded to 4 decimal places, df_between = k - 1 and df_within = N - k.
Input Table(s)
scores
| Column | Type | Meaning |
|---|
| grp | VARCHAR(5) | Group label: 'A', 'B' or 'C' |
| value | DECIMAL(10,4) | The subject's measured value |
There is no primary key. Each row is one subject's observation, and two rows can be identical.
Output Contract
One row with exactly these columns, in this order:
| Column | Type | Meaning |
|---|
| f_statistic | NUMERIC | The ANOVA F statistic, rounded to 4 decimal places |
| df_between | INTEGER | k - 1 |
| df_within | INTEGER | N - k |
The result always has exactly one row, so it needs no ordering.
Example
Input scores:
| grp | value |
|---|
| C | 25.4 |
| B | 22.9 |
| B | 17.1 |
| C | 20.1 |
| A | 10.2 |
| C | 28.7 |
| A | 14.1 |
| A | 12.7 |
| A | 11.3 |
| C | 22.2 |
| C | 25.7 |
| B | 17.9 |
| B | 22.6 |
| A | 16.9 |
| B | 21.2 |
Output:
| f_statistic | df_between | df_within |
|---|
| 19.8267 | 2 | 12 |
Each group has 5 rows, with means 13.04 (A), 20.34 (B) and 24.42 (C), and the grand mean is 289 / 15 = 19.26667. That gives SSB = 332.40133 and SSW = 27.232 + 28.852 + 44.508 = 100.592. With k = 3 and N = 15, F = (332.40133 / 2) / (100.592 / 12) = 166.20067 / 8.38267 = 19.82671.
Constraints and Clarifications
-
Neither column contains NULL.
-
k
is the number of distinct
grp
values present, and
N
counts every row.
-
Group sizes can differ. The grand mean is the mean over all rows, which equals the plain average of the group means only when every group has the same number of rows.
-
Identical rows are separate subjects. Each one counts toward
ng
,
N
, the means and both sums.
-
Every input has at least two groups, more rows than groups, and some variation inside at least one group, so SSW is positive and F is defined.
-
Round only the final F. The degrees of freedom are integers.