Average first purchase value by campaign, organic included
The task
The growth team wants to compare how much users spend on their first order, by where they came from. A user's first purchase is their earliest purchased_at in purchases. If two purchases share the same purchased_at, the one with the lower purchase_id is first. Users with no purchase are ignored.
Group users by the campaign credited in signups.campaign_id. Sign-ups with a NULL campaign_id are organic and are shown under the label Organic; every other group uses the campaign_name.
Return campaign (the name or Organic), buyers (users whose first purchase falls in the group) and avg_first_purchase (the average amount of those first purchases, rounded to 2 decimal places). Sort by buyers descending, then campaign ascending.
Expected output
campaignbuyersavg_first_purchase- Sorted by buyers descending, then campaign ascending.
- Numbers rounded to 2 decimal places.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Marketing team · click a name to insert it
campaigns10 rows in the sample
One row per campaign. Key: campaign_id.
| int | Unique id. | |
| text | Name. | |
| text | search, social, video, email or affiliate. | |
| date | First day live. | |
| date | Last day live. NULL if still running. | |
| numeric(10,2) | Budget per day, USD. |
ad_daily969 rows in the sample
One row per campaign per day it served ads. Days with no delivery have no row. Key: campaign_id, day.
| int | The campaign. | |
| date | The day. | |
| int | Times an ad was shown. | |
| int | Clicks. | |
| numeric(10,2) | Money spent, USD. |
signups2,108 rows in the sample
One row per new user. campaign_id is the campaign credited with the sign-up. Key: user_id.
| int | Unique id. | |
| timestamp | When they signed up (UTC). | |
| int | Credited campaign. NULL for organic sign-ups. | |
| text | Two-letter country code. |
purchases2,294 rows in the sample
One row per paid order by a user. Key: purchase_id.
| int | Unique id. | |
| int | Buyer. | |
| timestamp | When (UTC). | |
| numeric(10,2) | Order value, USD. |
Related questions
- Driver rating leaderboard, unrated drivers lastmedium
- Each customer's largest single expense, with tie-breakersmedium
- Favourite spending category per customer, counted by segmenthard
- Genre of each listener's first playmedium
- Repeat contact within 14 days of a first tickethard
- Three slowest first responses in each channeleasy
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL