Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Marketing reports revenue on last-touch attribution: a conversion is credited to the campaign of the user's most recent click within 7 days before it. A conversion with no qualifying click is direct — unattributed, not deleted.
Return one row per campaign plus a direct row, ordered by attributed value descending.
Result columns · in this order
campaign | Campaign credited, or direct when nothing qualified. |
conversions | Conversions credited to it. |
attributed_value | Value credited to it. |
How to approach it
Run the starter and count how many rows conversion 101 produces. Attribution has to pick one.
Sample input
| conversion_id | user_id | converted_at | conversion_value |
|---|---|---|---|
| 101 | g1 | 2026-02-13 | 120 |
| 102 | g2 | 2026-02-06 | 80 |
| 103 | g3 | 2026-02-01 | 200 |
| 104 | g4 | 2026-02-24 | 150 |
| 105 | g5 | 2026-02-20 | 90 |
| 106 | g6 | 2026-02-28 | 60 |
| 107 | g8 | 2026-02-26 | 40 |
| 108 | g1 | 2026-02-10 | 30 |
| 109 | g3 | 2026-02-21 | 110 |
9 rows — all rows shown.
| click_id | user_id | campaign | clicked_at |
|---|---|---|---|
| 1 | g1 | brand-search | 2026-02-01 |
| 2 | g1 | shopping | 2026-02-09 |
| 3 | g1 | display | 2026-02-12 |
| 4 | g2 | brand-search | 2026-02-03 |
| 5 | g2 | shopping | 2026-02-05 |
| 6 | g3 | display | 2026-01-20 |
| 7 | g3 | shopping | 2026-02-18 |
| 8 | g4 | brand-search | 2026-02-22 |
| 9 | g5 | display | 2026-02-02 |
| 10 | g6 | shopping | 2026-02-25 |
| 11 | g6 | brand-search | 2026-02-27 |
| 12 | g7 | display | 2026-03-01 |
12 rows — scroll inside the table to see them all.
Expected output
| campaign | conversions | attributed_value |
|---|---|---|
| direct | 3 | 330 |
| shopping | 3 | 220 |
| brand-search | 2 | 210 |
| display | 1 | 120 |
4 rows — all rows shown.
Constraints
converted_at with at most 7 days between them.'direct'.attributed_value is the sum of conversion_value for the conversions credited to that campaign, rounded to 2 decimal places.attributed_value descending, then campaign.Worked example
User g1 clicked brand-search on 1 February, shopping on the 9th and display on the 12th, then converted for 120 on the 13th. Last touch inside the window is display, so display gets 120 and the other two campaigns get nothing.
Join every earlier click instead and that one conversion is counted three times — 120 credited to each of three campaigns. Across the whole dataset the naive join reports 14 conversions worth 1360 against a real 9 worth 880. Conversions 103, 105 and 107 make the opposite point: an inner join drops them, so 330 of genuine revenue disappears rather than being labelled direct.
What this tests
Picking exactly one related row per fact, in the presence of a time window — and the double failure of the obvious join, which multiplies the conversions that have clicks and deletes the ones that do not.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.