Skip to content

Number campaigns by end date, running ones last

MediumFull bankNULL handlingRanking and top N10 min2 hidden datasets

The task

The campaign dashboard lists campaigns by how recently they ended. Number the campaigns 1, 2, 3, ... in this order:

  1. Campaigns that have ended come first, the most recently ended first (ended_on latest to earliest).
  2. Campaigns that are still running (ended_on is NULL) come after all the ended ones.
  3. Campaigns with the same ended_on, including all the running ones, are ordered by campaign_id ascending.

Every campaign gets a different number. Return campaign_name and display_position, the number the campaign gets. Sort by display_position ascending.

Expected output

campaign_namedisplay_position
  • Sorted by display_position ascending.
  • 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.

intUnique id.
textName.
textsearch, social, video, email or affiliate.
dateFirst day live.
dateLast 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.

intThe campaign.
dateThe day.
intTimes an ad was shown.
intClicks.
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.

intUnique id.
timestampWhen they signed up (UTC).
intCredited campaign. NULL for organic sign-ups.
textTwo-letter country code.
purchases2,294 rows in the sample

One row per paid order by a user. Key: purchase_id.

intUnique id.
intBuyer.
timestampWhen (UTC).
numeric(10,2)Order value, USD.

Related questions

Loading the editor and PostgreSQL