Skip to content

Accounts with a high average invoice

MediumFull bankGroup and aggregateDates and time12 min2 hidden datasets

The task

The revenue team wants a shortlist of big-ticket accounts. Use every invoice in invoices, paid or not.

Keep the accounts whose average invoice amount is 500 or more. The average is taken over all of the account's invoices, not only the large ones.

Return:

  • account_id
  • invoices: how many invoices the account has
  • avg_invoice: the average amount, rounded to 2 decimal places
  • first_issued: the earliest issued_on
  • last_issued: the latest issued_on
  • unpaid: how many of the account's invoices have no paid_on

Sort by avg_invoice descending, then account_id ascending.

Expected output

account_idinvoicesavg_invoicefirst_issuedlast_issuedunpaid
  • Sorted by avg_invoice descending, then account_id 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