Given a table of user clicks and a table of conversions, write a query to attribute conversions to the last clicked ad before the purchase event at Affirm.
Use only clicks strictly before each purchase. If multiple eligible clicks have the same timestamp, choose the greatest click_id. Include conversions with no eligible click.
purchase_at, then conversion_idconversion_id, user_id, purchase_at, conversion_amount, attributed_ad_id, attributed_click_id, and click_at| Column | Type | Description |
|---|---|---|
| click_idPK | INT | Unique click identifier |
| user_id | INT | User who clicked the ad |
| ad_id | VARCHAR(50) | Identifier of the clicked ad |
| clicked_at | TIMESTAMP | Timestamp when the ad was clicked |
| Column | Type | Description |
|---|---|---|
| conversion_idPK | INT | Unique purchase conversion identifier |
| user_id | INT | User who completed the purchase |
| purchase_at | TIMESTAMP | Timestamp when the purchase occurred |
| conversion_amount | DECIMAL(10,2) | Purchase amount |