Write a PostgreSQL query that computes a two-sample z-statistic for the difference between control and treatment means and flags whether it passes the 1.96 cutoff. It tests conditional aggregation, sample variance and careful rounding in SQL.
Table `measurements` holds one continuous observation per row in `value`, labeled by `group_name`. Rows with `group_name = 'control'` are group 1 and rows with `group_name = 'treatment'` are group 2. Using each group's sample variance as if it were the population variance, compute the two-sample z-statistic
$$
z = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_1^2 / n_1 + s_2^2 / n_2}}
$$
Return one row: `z_statistic` (rounded to 4 decimals) and `significant` (1 if $|z| > 1.96$, else 0).
```hint Watch the denominators
Use the sample variance with the $n - 1$ denominator, and make sure the count you divide by covers exactly the rows that went into each group's mean and variance.
```
### Input Table(s)
Table `measurements`:
| Column | Type | Meaning |
|---|---|---|
| group_name | VARCHAR(20) | `'control'` (group 1) or `'treatment'` (group 2) |
| value | DECIMAL(10,4) | The measured outcome for one observation |
The table has no primary key. Each row is one observation, and identical rows are separate observations.
### Output Contract
Exactly one row with these columns:
| Column | Type | Meaning |
|---|---|---|
| z_statistic | numeric | The z-statistic, control minus treatment, rounded to 4 decimal places |
| significant | integer | 1 when the absolute value of the unrounded z is greater than 1.96, else 0 |
The result always has one row, so no ordering is needed.
### Example
```text
Input: measurements
+------------+-------+
| group_name | value |
+------------+-------+
| treatment | 48.1 |
| control | 53.2 |
| treatment | 52.9 |
| control | 47.9 |
| control | 48 |
| control | 45.6 |
| control | 47.4 |
| treatment | 50 |
| treatment | 49.8 |
| control | 46.7 |
| control | 52.7 |
| treatment | 52.2 |
| treatment | 48.4 |
| treatment | 49.4 |
| treatment | 57.1 |
| control | 53.1 |
| control | 46.5 |
| treatment | 53.5 |
+------------+-------+
Output:
+-------------+-------------+
| z_statistic | significant |
+-------------+-------------+
| -1.5946 | 0 |
+-------------+-------------+
```
Each group has 9 rows. Control has mean 49.0111 and sample variance 9.5011, treatment has mean 51.2667 and sample variance 8.5050, so the standard error is about 1.4145 and z is about -1.5946. Its absolute value is below 1.96, so `significant` is 0.
### Constraints and Clarifications
- `group_name` only takes the values `'control'` and `'treatment'`.
- `value` is never NULL.
- Each group has at least two rows, and the standard error is greater than zero.
- Sample variance uses the $n - 1$ denominator.
- Duplicate rows all count as observations.
- The cutoff is strict and uses the unrounded z: an absolute value of exactly 1.96 gives 0.
Overview: Write a PostgreSQL query that computes a two-sample z-statistic for the difference between control and treatment means and flags whether it passes the 1.96 cutoff. It tests conditional aggregation, sample variance and careful rounding in SQL.
Table measurements holds one continuous observation per row in value, labeled by group_name. Rows with group_name = 'control' are group 1 and rows with group_name = 'treatment' are group 2. Using each group's sample variance as if it were the population variance, compute the two-sample z-statistic
z=s12/n1+s22/n2xˉ1−xˉ2
Return one row: z_statistic (rounded to 4 decimals) and significant (1 if ∣z∣>1.96, else 0).
Input Table(s)
Table measurements:
Column
Type
Meaning
group_name
VARCHAR(20)
'control' (group 1) or 'treatment' (group 2)
value
DECIMAL(10,4)
The measured outcome for one observation
The table has no primary key. Each row is one observation, and identical rows are separate observations.
Output Contract
Exactly one row with these columns:
Column
Type
Meaning
z_statistic
numeric
The z-statistic, control minus treatment, rounded to 4 decimal places
significant
integer
1 when the absolute value of the unrounded z is greater than 1.96, else 0
The result always has one row, so no ordering is needed.
Each group has 9 rows. Control has mean 49.0111 and sample variance 9.5011, treatment has mean 51.2667 and sample variance 8.5050, so the standard error is about 1.4145 and z is about -1.5946. Its absolute value is below 1.96, so significant is 0.
Constraints and Clarifications
group_name
only takes the values
'control'
and
'treatment'
.
value
is never NULL.
Each group has at least two rows, and the standard error is greater than zero.
Sample variance uses the
n−1
denominator.
Duplicate rows all count as observations.
The cutoff is strict and uses the unrounded z: an absolute value of exactly 1.96 gives 0.