Table cohort has one row per subject, with a group_name ('exposed' or 'unexposed') and an outcome flag (1 = event, 0 = no event). Compute each group's risk as its number of events divided by its number of subjects:
pe=nexe,pu=nuxu
Return one row with relative_risk =pe/pu and risk_difference =pe−pu, each rounded to 4 decimals.
Input Table(s)
cohort
| Column | Type | Meaning |
|---|
group_name | VARCHAR(20) | Group of the subject, 'exposed' or 'unexposed' |
outcome | SMALLINT | 1 if the subject had the event, 0 if not |
The table has no key. Each row is one subject, so identical rows are normal.
Output Contract
-
Exactly one row with two columns, in this order:
relative_risk
,
risk_difference
.
-
relative_risk
is
pe/pu
rounded to 4 decimal places. It is NULL when either group has no subjects or when the unexposed group has no events.
-
risk_difference
is
pe−pu
rounded to 4 decimal places. It is NULL when either group has no subjects, and it can be negative.
Example
cohort (21 rows):
| group_name | outcome |
|---|
| exposed | 0 |
| unexposed | 0 |
| unexposed | 1 |
| exposed | 1 |
| unexposed | 0 |
| exposed | 1 |
| unexposed | 0 |
| unexposed | 0 |
| unexposed | 1 |
| unexposed | 1 |
| exposed | 1 |
| exposed | 0 |
| exposed | 0 |
| unexposed | 0 |
| unexposed | 1 |
| exposed | 0 |
| exposed | 0 |
| exposed | 1 |
| exposed | 1 |
| unexposed | 0 |
| unexposed | 0 |
Expected result:
| relative_risk | risk_difference |
|---|
| 1.3750 | 0.1364 |
The exposed group has 5 events among 10 subjects (pe=0.5) and the unexposed group has 4 events among 11 subjects (pu≈0.3636). So the relative risk is 0.5/(4/11)=55/40=1.375 and the risk difference is 0.5−4/11=15/110≈0.1364.
Constraints and Clarifications
-
group_name
is never NULL and is always
'exposed'
or
'unexposed'
.
outcome
is never NULL and is always 0 or 1.
-
Count every row. Duplicate rows are separate subjects.
-
Compute both outputs from the exact risks and round only the final values.
-
If the table is empty, return one row with both columns NULL.