← Practice

Buyers making up 80% of revenue: countries, Feb 2025

Level: SeniorTopic: CTE200 XP

Instructions

For each value of users.country (country) consider the buyers of this group. A buyer's revenue is the sum of purchases.amount of their purchases made on or after February 1, 2025 and strictly before March 1, 2025. The group's total revenue is the sum of the revenues of its buyers for the same period (the share is computed from the revenue of the buyer's own group, not of all users together). Order the group's buyers by revenue descending (for equal revenue, by the smaller user_id) and take the minimum number of the first of them whose total revenue is at least 80% of the group's total revenue. Output country, buyers_cnt (how many buyers the group has in total), users_cnt (how many buyers are in this selection) and revenue, the total revenue of these buyers, rounded to 2 decimal places. Do not output groups without purchases. 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

events.user_id (0…N) ↔ users.id (1…1)purchases.user_id (0…N) ↔ users.id (1…1)eventsPKidintegerFKuser_idintegerpurchasesPKidintegerFKuser_idintegerusersPKidinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-oneevents.user_id → users.id
  • N:1Many-to-onepurchases.user_id → users.id

Table data

PostgreSQL

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

Dataset schema