Compute a one-sample z-statistic in SQL when the population standard deviation is known, testing a sample mean against a hypothesized value of 250. It tests putting a fixed sigma in the standard error, using aggregates for the mean and sample size, and numeric rounding in PostgreSQL.
Table `samples` has one numeric column `measurement`. With known population standard deviation 40 and hypothesized mean 250, compute
$$z = \frac{\text{mean} - 250}{40 / \sqrt{n}}$$
where `n` is the number of rows. Return one row: `z_statistic` (rounded to 4 decimals) and `n` (sample size).
```hint Which spread goes in the denominator
The population standard deviation is given, so nothing about the spread has to be estimated from the table. Check the expression's type before you call PostgreSQL's two-argument ROUND.
```
### Input Table(s)
Table `samples`
| Column | Type | Meaning |
|---|---|---|
| `measurement` | NUMERIC(10,4) | one observed value |
Keys: none. Each row is one observation.
### Output Contract
Exactly one row with these columns, in this order:
| Column | Type | Meaning |
|---|---|---|
| `z_statistic` | NUMERIC | the z statistic rounded to 4 decimal places |
| `n` | INTEGER | the number of rows in `samples` |
The result is a single row, so no ordering is needed.
### Example
Input `samples`:
| measurement |
|---|
| 229 |
| 245.6 |
| 232 |
| 238.3 |
| 254.5 |
| 229.2 |
| 226.3 |
| 236.4 |
| 266.3 |
Expected output:
| z_statistic | n |
|---|---|
| -0.7700 | 9 |
The 9 measurements sum to 2157.6, so the mean is about 239.7333, which is 92.4 / 9 (about 10.2667) below 250. The standard error is ${40 / \sqrt{9}} \approx 13.3333$, and $z = -10.2667 / 13.3333$, which is exactly -0.77 and shows as -0.7700 at 4 decimals.
### Constraints and Clarifications
- `samples` has at least one row.
- `measurement` is never NULL.
- The population standard deviation is 40 and the hypothesized mean is 250. Neither is stored in a table.
- Round `z_statistic` with PostgreSQL `ROUND(numeric, 4)`, applied to the final statistic only.
Overview: Compute a one-sample z-statistic in SQL when the population standard deviation is known, testing a sample mean against a hypothesized value of 250. It tests putting a fixed sigma in the standard error, using aggregates for the mean and sample size, and numeric rounding in PostgreSQL.
Table samples has one numeric column measurement. With known population standard deviation 40 and hypothesized mean 250, compute
z=40/nmean−250
where n is the number of rows. Return one row: z_statistic (rounded to 4 decimals) and n (sample size).
Input Table(s)
Table samples
Column
Type
Meaning
measurement
NUMERIC(10,4)
one observed value
Keys: none. Each row is one observation.
Output Contract
Exactly one row with these columns, in this order:
Column
Type
Meaning
z_statistic
NUMERIC
the z statistic rounded to 4 decimal places
n
INTEGER
the number of rows in samples
The result is a single row, so no ordering is needed.
Example
Input samples:
measurement
229
245.6
232
238.3
254.5
229.2
226.3
236.4
266.3
Expected output:
z_statistic
n
-0.7700
9
The 9 measurements sum to 2157.6, so the mean is about 239.7333, which is 92.4 / 9 (about 10.2667) below 250. The standard error is 40/9≈13.3333, and z=−10.2667/13.3333, which is exactly -0.77 and shows as -0.7700 at 4 decimals.
Constraints and Clarifications
samples
has at least one row.
measurement
is never NULL.
The population standard deviation is 40 and the hypothesized mean is 250. Neither is stored in a table.
Round
z_statistic
with PostgreSQL
ROUND(numeric, 4)
, applied to the final statistic only.