← Retention, funnels, cohorts

Spend 'utilities', premium: Apr 2025

Level: Middle100 XP

Instructions

A client's spend is their transactions of the category 'utilities' (transactions.category, the amounts are negative) across all their accounts from April 1, 2025 to April 30, 2025 inclusive; the spend amount is the absolute value of the sum of amount of these transactions (−sum(amount)). For clients of the segment 'premium', for each city (clients.city), output city, clients (the number of all clients of the segment in the city, including those with no spend or even no accounts), spenders (the number of clients with at least one such spend), spend (the sum of their spend, 0 if there is none), spend_per_client (spend / clients) and spend_per_spender (spend / spenders; NULL if spenders = 0); round both averages to 2 decimal places. Sort by city.

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