The scores table records a numeric value per subject, and each subject belongs to one group grp, which is 'A', 'B' or 'C'. Compute the between-group sum of squares (SSB) and the within-group sum of squares (SSW):
SSB=∑gng(xˉg−xˉ)2
SSW=∑g∑i∈g(xi−xˉg)2
Here ng and xˉg are the row count and mean value of group g, xi is one row's value, and xˉ is the grand mean, the mean of value over all rows. Return one row with ssb and ssw, each rounded to 4 decimal places.
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 |
|---|
| ssb | NUMERIC | Between-group sum of squares, rounded to 4 decimal places |
| ssw | NUMERIC | Within-group sum of squares, rounded to 4 decimal places |
The result always has exactly one row, so it needs no ordering.
Example
Input scores:
| grp | value |
|---|
| A | 12.7 |
| B | 21.9 |
| C | 20.9 |
| C | 25 |
| B | 16.2 |
| C | 23.9 |
| A | 16.9 |
| B | 19.8 |
| A | 19.4 |
| A | 17.1 |
| B | 21.7 |
| C | 23.7 |
Output:
The group means are 16.525 (A), 19.9 (B) and 23.375 (C), and the grand mean is 239.2 / 12 = 19.93333. The squared gaps between each group mean and the grand mean are 11.61674, 0.00111 and 11.84507. Every group has 4 rows, so SSB is 4 times their sum, 93.85168, which rounds to 93.8517. SSW adds the squared distance of each value from its own group mean: 23.3675 for A, 20.94 for B and 9.1475 for C, a total of 53.455.
Constraints and Clarifications
-
Neither column contains NULL, and the table has at least one row.
-
Identical rows are separate subjects. Each one counts toward
ng
, both means and both sums.
-
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.
-
A group with a single row contributes 0 to SSW.
-
Round only the two final sums, not the means used to build them.