Skip to content

Months billed above the monthly average

EasyFreeCTEs and subqueriesGroup and aggregate10 min2 hidden datasets

The task

Finance wants to flag the strong billing months. A month's billed amount is the sum of invoices.amount for invoices whose issued_on falls in that calendar month. The average month is the plain average of those monthly billed amounts across all months that have at least one invoice.

Return the months whose billed amount is strictly greater than the average month, with:

  • month: the first day of the month
  • billed: the total billed in that month
  • avg_month: the average month, rounded to 2 decimal places (the same value on every row)

Sort by month.

Expected output

monthbilledavg_month
  • Sorted by month ascending.
  • Numbers rounded to 2 decimal places.
  • Checked on the sample and on 2 hidden datasets. How grading works

Tables B2B software company · click a name to insert it

accounts110 rows in the sample

One row per customer company. Key: account_id.

intUnique id.
textCompany name.
textRetail, Finance, Education, Media, Logistics, Software or Manufacturing.
textTwo-letter country code.
dateDay the account was created.
textName of the owner on our side. NULL when unassigned.
plans4 rows in the sample

Price list. Key: plan_id.

textstarter, team, business or enterprise.
textDisplay name.
numeric(10,2)Monthly price per seat, USD.
subscriptions149 rows in the sample

One row per stretch of time an account spent on one plan. A plan change ends one row and starts another on the same day. Key: subscription_id.

intUnique id.
intThe account.
textThe plan.
intSeats paid for.
dateFirst day on this plan.
dateDay it stopped (not included). NULL while active.
invoices1,090 rows in the sample

One invoice per account per month while subscribed. Key: invoice_id.

intUnique id.
intThe account billed.
dateIssue day.
datePayment due day.
numeric(10,2)Amount billed, USD.
dateDay it was paid. NULL if unpaid.

Related questions

Loading the editor and PostgreSQL