Write a SQL query that turns a table of per-user conversion flags into a 95% Agresti-Coull (adjusted-Wald) confidence interval for the conversion rate. It tests aggregation in a CTE, applying an interval formula to small samples, and numeric casting and rounding in PostgreSQL.
The `trials` table has one row per user with a `converted` flag (1 or 0). Return one row with the 95% adjusted-Wald (Agresti-Coull) interval for the conversion rate: `p_tilde`, `ci_lower` and `ci_upper`, each rounded to 4 decimals.
Use these definitions, where $n$ is the number of users, $x$ is the number of conversions and $z = 1.96$ (so $z^2 = 3.8416$):
- $\tilde{n} = n + 3.8416$
- $\tilde{p} = (x + 1.9208) / \tilde{n}$, returned as `p_tilde`
- $\tilde{p} - 1.96\sqrt{\tilde{p}(1 - \tilde{p}) / \tilde{n}}$, returned as `ci_lower`
- $\tilde{p} + 1.96\sqrt{\tilde{p}(1 - \tilde{p}) / \tilde{n}}$, returned as `ci_upper`
```hint Order of operations
Reduce the table to n and x in one aggregate, apply the formulas to those two numbers, and round only in the final SELECT.
```
### Input Table(s)
`trials`
| Column | Type | Meaning |
|---|---|---|
| converted | SMALLINT | 1 if the user converted, 0 if not |
There is no key column. Each row is one user, so the row count is the number of users.
### Output Contract
- Exactly one row with the columns `p_tilde`, `ci_lower`, `ci_upper`, in that order.
- Each value is rounded to 4 decimal places. Round only the final values, never the intermediate ones.
- The result has a single row, so no ordering is needed.
### Example
Input `trials` (12 users, 1 conversion):
| converted |
|---|
| 0 |
| 0 |
| 1 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
Expected output:
| p_tilde | ci_lower | ci_upper |
|---|---|---|
| 0.1844 | -0.0066 | 0.3753 |
Here $n = 12$ and $x = 1$, so $\tilde{n} = 15.8416$ and $\tilde{p} = 2.9208 / 15.8416 \approx 0.18438$. The half-width is ${1.96\sqrt{0.18438 \cdot 0.81562 / 15.8416} \approx 0.19096}$, which puts the bounds at -0.0066 and 0.3753 after rounding.
### Constraints and Clarifications
- `converted` is never NULL and holds only 0 or 1.
- The table has at least one row.
- Report the bounds exactly as the formula gives them. Do not clip `ci_lower` at 0 or `ci_upper` at 1; the negative lower bound in the example is the expected value.
- Use the constants 1.96, 3.8416 and 1.9208 as given.
- There are no ties or duplicates to resolve: the answer is a single aggregate row, and each row of `trials` is a distinct user.
Overview: Write a SQL query that turns a table of per-user conversion flags into a 95% Agresti-Coull (adjusted-Wald) confidence interval for the conversion rate. It tests aggregation in a CTE, applying an interval formula to small samples, and numeric casting and rounding in PostgreSQL.
The trials table has one row per user with a converted flag (1 or 0). Return one row with the 95% adjusted-Wald (Agresti-Coull) interval for the conversion rate: p_tilde, ci_lower and ci_upper, each rounded to 4 decimals.
Use these definitions, where n is the number of users, x is the number of conversions and z=1.96 (so z2=3.8416):
n~=n+3.8416
p~=(x+1.9208)/n~
, returned as
p_tilde
p~−1.96p~(1−p~)/n~
, returned as
ci_lower
p~+1.96p~(1−p~)/n~
, returned as
ci_upper
Input Table(s)
trials
Column
Type
Meaning
converted
SMALLINT
1 if the user converted, 0 if not
There is no key column. Each row is one user, so the row count is the number of users.
Output Contract
Exactly one row with the columns
p_tilde
,
ci_lower
,
ci_upper
, in that order.
Each value is rounded to 4 decimal places. Round only the final values, never the intermediate ones.
The result has a single row, so no ordering is needed.
Example
Input trials (12 users, 1 conversion):
converted
0
0
1
0
0
0
0
0
0
0
0
0
Expected output:
p_tilde
ci_lower
ci_upper
0.1844
-0.0066
0.3753
Here n=12 and x=1, so n~=15.8416 and p~=2.9208/15.8416≈0.18438. The half-width is 1.960.18438⋅0.81562/15.8416≈0.19096, which puts the bounds at -0.0066 and 0.3753 after rounding.
Constraints and Clarifications
converted
is never NULL and holds only 0 or 1.
The table has at least one row.
Report the bounds exactly as the formula gives them. Do not clip
ci_lower
at 0 or
ci_upper
at 1; the negative lower bound in the example is the expected value.
Use the constants 1.96, 3.8416 and 1.9208 as given.
There are no ties or duplicates to resolve: the answer is a single aggregate row, and each row of
trials
is a distinct user.