Top two agents per team by satisfaction, with tie-breakers
The task
The support lead wants to recognise the two happiest-making agents in each team. Use the surveys in satisfaction joined to their ticket's agent_id. An agent is eligible with at least 15 survey answers in total. Tickets with no agent or no survey are not counted.
Rank the eligible agents inside their team by exact average score, highest first. If two agents have the same average, the one with more survey answers comes first. If they are still tied, the lower agent_id comes first. Everyone gets a distinct position, and the top 2 of each team are returned. A team with fewer than two eligible agents returns fewer rows.
Return team, full_name, surveys (the number of survey answers), avg_score (the average score rounded to 2 decimal places) and team_position (1 or 2). Sort by team ascending, then team_position ascending.
Expected output
teamfull_namesurveysavg_scoreteam_position- Sorted by team ascending, then team_position ascending.
- Numbers rounded to 2 decimal places.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Customer support desk · click a name to insert it
agents14 rows in the sample
Support agents. Key: agent_id.
| int | Unique id. | |
| text | Name. | |
| text | billing, technical or general. | |
| date | Start day. |
tickets900 rows in the sample
One row per ticket. Key: ticket_id.
| int | Unique id. | |
| int | Who opened it. | |
| int | Assigned agent. NULL if never assigned. | |
| text | email, chat or phone. | |
| text | low, normal, high or urgent. | |
| text | billing, bug, how_to or account. | |
| timestamp | Opened (UTC). | |
| timestamp | First reply from an agent. NULL if none yet. | |
| timestamp | Closed as solved. NULL while open. |
satisfaction366 rows in the sample
Survey answers, at most one per ticket. Key: ticket_id.
| int | The ticket. | |
| int | 1 (very unhappy) to 5 (very happy). | |
| timestamp | When answered (UTC). |
Related questions
- Favourite spending category per customer, counted by segmenthard
- First order value by acquisition channelmedium
- How far each artist climbed or fell from April to Mayhard
- Listener quartiles by May listening timemedium
- Months each merchant ranked in the top three for spendmedium
- Repeat contact within 14 days of a first tickethard
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL