← Window functions

NTILE(5), group 1: spending, USD, Aug–Nov 2025

Level: Middle100 XP

Instructions

A client's spending is the sum of -amount over their transactions with amount < 0 on accounts in the currency USD (accounts.currency), with tx_time on or after August 1, 2025 and strictly before December 1, 2025; clients without such transactions do not participate. Split the clients into 5 groups with NTILE(5) by spending descending (for equal spending, by client id ascending): group 1 has the largest spending, group 5 the smallest; if the number of clients is not divisible by 5, the groups with smaller numbers have one more client than the others. Output the clients of group 1: id, name, spend. Row order does not matter.

Hints

Each next one is more detailed and costs more

Log in to open hints

Query

Result

Click Run to see the query result, or Check to submit your solution.

Dataset schema

accounts.client_id (0…N) ↔ clients.id (1…1)transactions.account_id (0…N) ↔ accounts.id (1…1)accountsPKidintegerFKclient_idintegerclientsPKidintegertransactionsPKidintegerFKaccount_idinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-oneaccounts.client_id → clients.id
  • N:1Many-to-onetransactions.account_id → accounts.id

Table data

PostgreSQL

Select a table in the diagram or list to view its rows.

Dataset schema