Flag contingency table cells with expected counts below 5 in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The `crosstab_data` table has one row per subject, with a row category `row_cat` (`'A'`, `'B'` or `'C'`) and a column category `col_cat` (`'X'`, `'Y'` or `'Z'`). Before running a chi-square test of independence on this contingency table, you want to see which cells have small expected counts.
Under independence, a cell's expected count is the row total times the column total divided by the grand total:
$$E_{ij} = \frac{R_i \cdot C_j}{N}$$
Return one row per cell with `row_cat`, `col_cat`, `expected` rounded to 4 decimals, and `below_5`, which is 1 when the expected count is below 5 and 0 otherwise.
```hint Totals first
Compute the row, column and grand totals separately before you form the cells. Decide the flag from the expected count before it is rounded.
```
### Input Table(s)
`crosstab_data`
| Column | Type | Meaning |
|---|---|---|
| `row_cat` | VARCHAR(5) | Row category of the subject, `'A'`, `'B'` or `'C'` |
| `col_cat` | VARCHAR(5) | Column category of the subject, `'X'`, `'Y'` or `'Z'` |
The table has no key. Each row is one subject, so identical rows are normal.
### Output Contract
- Columns, in this order: `row_cat`, `col_cat`, `expected`, `below_5`.
- `expected` is the cell's expected count, rounded to 4 decimal places.
- `below_5` is the integer 1 when the exact expected count is less than 5, else 0.
- One row for every pairing of a `row_cat` value with a `col_cat` value that appears in the table, including a pairing that no subject has.
- Sort by `row_cat` ascending, then `col_cat` ascending. Each pair appears once, so this order is unique.
### Example
`crosstab_data` (17 rows):
| row_cat | col_cat |
|---|---|
| C | Z |
| C | X |
| B | Y |
| C | X |
| C | Y |
| A | Y |
| A | Y |
| B | X |
| B | Z |
| C | Y |
| B | Y |
| C | X |
| C | Y |
| A | Z |
| A | X |
| A | X |
| B | Y |
The row totals are A = 5, B = 5 and C = 7. The column totals are X = 6, Y = 8 and Z = 3, and $N = 17$.
Expected result:
| row_cat | col_cat | expected | below_5 |
|---|---|---|---|
| A | X | 1.7647 | 1 |
| A | Y | 2.3529 | 1 |
| A | Z | 0.8824 | 1 |
| B | X | 1.7647 | 1 |
| B | Y | 2.3529 | 1 |
| B | Z | 0.8824 | 1 |
| C | X | 2.4706 | 1 |
| C | Y | 3.2941 | 1 |
| C | Z | 1.2353 | 1 |
The largest expected count is cell (C, Y) at ${7 \cdot 8 / 17 \approx 3.2941}$, so every cell is flagged. Cell (A, Z) is ${5 \cdot 3 / 17 \approx 0.8824}$.
### Constraints and Clarifications
- `row_cat` and `col_cat` are never NULL.
- Count every row. Duplicate rows are separate subjects.
- The flag uses the exact expected count, before rounding. A cell whose expected count is exactly 5 gets `below_5 = 0`.
- A category value that no subject has (for example, no `'C'` rows at all) has no total and produces no output rows.
- Several cells can share an expected count (A, X and B, X above). The sort on the category pair keeps the order unique.
- If `crosstab_data` is empty, return no rows.
Overview: Write a PostgreSQL query that computes the expected count of every cell in a 3x3 contingency table and flags cells whose expected count is below 5. It tests marginal totals, keeping cells nobody falls into, exact threshold checks and the chi-square sample size rule of thumb.