Retour à la bibliothèque
Atelier · Programmation · 5 sections

Préparer des tables analytiques en SQL

Agrégez des résultats et repérez les lignes dupliquées par une jointure SQL.

La lecture en ligne ne nécessite pas de compte.

STATISTICAL PROGRAMMING LAB

Who had a return visit within 30 days?

Six people, two groups, and a visit log with repeat visits. Build an endpoint, compare risks, then audit three proposed pieces of code.

Inspect the source tables

Day 0 is enrollment. Follow-up is the number of days with complete capture of return visits. The visit log can include records outside the endpoint window.

cohort

idarmfollowup
1A30
2A30
3A12
4B30
5B30
6B30

visits

idday
18
112
231
430
60

SQL workbench

Return six rows ordered by id, with columns id and event. Count a visit on days 1 through 30 inclusive. A person with no qualifying visit needs 30 days of follow-up to be classified as event-free. Otherwise the outcome is unknown.

Explain the group comparison

With 12 days observed, person 3 has an unknown outcome. Group A has one event among two observed people (50%). With 30 days and no visit, that person becomes an observed zero and group A falls to one in three. Group B stays at one in three. The change comes entirely from outcome ascertainment.

Code review desk

events = [x for x in outcomes if x]
risk = sum(events) / len(events)

Which issue or review action matters here?

Create a small table

Use SQLite to reproduce the same target quantity. Create a new database or in-memory session and execute the statements. The primary key defines one row per person. The CHECK constraint limits observed outcomes to 0 or 1, while NULL represents an unobserved outcome.

CREATE TABLE outcomes (
  id INTEGER PRIMARY KEY,
  event INTEGER CHECK (event IN (0, 1))
);
INSERT INTO outcomes VALUES (1, 1), (2, 0), (3, NULL), (4, 0), (5, 1);

Choose which rows count

The expected row is enrolled 5, observed 4, events 2, missing 1, risk 0.50. COUNT(column) excludes NULL, while COUNT(*) includes every row. NULLIF prevents division by zero and returns an undefined risk when no outcomes are observed.

SELECT COUNT(*) AS enrolled,
       COUNT(event) AS observed,
       COALESCE(SUM(event), 0) AS events,
       COUNT(*) - COUNT(event) AS missing,
       1.0 * SUM(event) / NULLIF(COUNT(event), 0) AS risk
FROM outcomes;

Audit a one-to-many join

A join can change the row unit. Here person 1 has two visit records and person 2 has one. The joined table has three rows although only two people qualify. If the question is person-level risk among people with a visit, use EXISTS so each eligible person appears once. Compare the two counts before calculating any summary.

WITH visits(id) AS (VALUES (1), (1), (2))
SELECT COUNT(*) AS joined_rows,
       COUNT(DISTINCT o.id) AS people
FROM outcomes AS o
JOIN visits AS v ON o.id = v.id;

WITH visits(id) AS (VALUES (1), (1), (2))
SELECT COUNT(*) AS eligible_people,
       SUM(o.event) AS events,
       1.0 * SUM(o.event) / COUNT(o.event) AS observed_risk
FROM outcomes AS o
WHERE EXISTS (SELECT 1 FROM visits AS v WHERE v.id = o.id);

Practice with a cohort restriction

To study participants with a visit, use WHERE EXISTS with a correlated visit lookup on participant ID. The two eligible people each contribute once, giving one event among two observed outcomes. Compare that with two event rows among three joined rows.

Practice SQL quality checks

Write a query that finds duplicate IDs before a join. Then add a person with a missing outcome and confirm that enrolled and observed counts change differently. Save the cohort definition and SQL query together so another analyst can see how eligibility was determined.

Discussion

Questions, précisions et applications de cette ressource.

Chargement des commentaires…