Table exposure_log has one row per subject, with an exposure group ('exposed' or 'unexposed') and an outcome flag (1 = event, 0 = no event). Arrange the subjects in a 2x2 table:
| event (1) | no event (0) |
|---|
| exposed | a | b |
| unexposed | c | d |
Compute the odds ratio
OR=b⋅ca⋅d
and return one row with odds_ratio rounded to 4 decimals.
Input Table(s)
exposure_log
| Column | Type | Meaning |
|---|
exposure | VARCHAR(20) | Exposure 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 one column,
odds_ratio
.
-
odds_ratio
is
(a⋅d)/(b⋅c)
rounded to 4 decimal places.
-
When
b⋅c=0
the ratio is undefined or infinite, and
odds_ratio
is NULL.
Example
exposure_log (18 rows):
| exposure | outcome |
|---|
| unexposed | 0 |
| unexposed | 0 |
| exposed | 0 |
| unexposed | 1 |
| unexposed | 1 |
| unexposed | 1 |
| unexposed | 0 |
| exposed | 1 |
| exposed | 1 |
| exposed | 1 |
| exposed | 0 |
| exposed | 1 |
| exposed | 0 |
| exposed | 1 |
| unexposed | 0 |
| unexposed | 1 |
| exposed | 1 |
| unexposed | 1 |
Expected result:
The cells are a=6, b=3, c=5 and d=4, so the odds ratio is (6⋅4)/(3⋅5)=24/15=1.6.
Constraints and Clarifications
-
exposure
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.
-
Use exact division and round only the final value.
-
If
a
or
d
is 0 while
b⋅c>0
, the odds ratio is 0.
-
If the table is empty, all four counts are 0, so return one row with NULL.