Top Three Wineries Problem
Description
LeetCode Problem 2991.
Table: Wineries
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| country | varchar |
| points | int |
| winery | varchar |
+-------------+----------+
id is column of unique values for this table.
This table contains id, country, points, and winery.
Write a solution to find the top three wineries in each country based on their total points. If multiple wineries have the same total points, order them by winery name in ascending order. If there's no second winery, output 'No second winery,' and if there's no third winery, output 'No third winery.'
Return the result table ordered by country in ascending order*.*
The result format is in the following example.
Example 1:
Input:
Wineries table:
+-----+-----------+--------+-----------------+
| id | country | points | winery |
+-----+-----------+--------+-----------------+
| 103 | Australia | 84 | WhisperingPines |
| 737 | Australia | 85 | GrapesGalore |
| 848 | Australia | 100 | HarmonyHill |
| 222 | Hungary | 60 | MoonlitCellars |
| 116 | USA | 47 | RoyalVines |
| 124 | USA | 45 | Eagle'sNest |
| 648 | India | 69 | SunsetVines |
| 894 | USA | 39 | RoyalVines |
| 677 | USA | 9 | PacificCrest |
+-----+-----------+--------+-----------------+
Output:
+-----------+---------------------+-------------------+----------------------+
| country | top_winery | second_winery | third_winery |
+-----------+---------------------+-------------------+----------------------+
| Australia | HarmonyHill (100) | GrapesGalore (85) | WhisperingPines (84) |
| Hungary | MoonlitCellars (60) | No second winery | No third winery |
| India | SunsetVines (69) | No second winery | No third winery |
| USA | RoyalVines (86) | Eagle'sNest (45) | PacificCrest (9) |
+-----------+---------------------+-------------------+----------------------+
Explanation
For Australia
- HarmonyHill Winery accumulates the highest score of 100 points in Australia.
- GrapesGalore Winery has a total of 85 points, securing the second-highest position in Australia.
- WhisperingPines Winery has a total of 80 points, ranking as the third-highest.
For Hungary
- MoonlitCellars is the sole winery, accruing 60 points, automatically making it the highest. There is no second or third winery.
For India
- SunsetVines is the sole winery, earning 69 points, making it the top winery. There is no second or third winery.
For the USA
- RoyalVines Wines accumulates a total of 47 + 39 = 86 points, claiming the highest position in the USA.
- Eagle'sNest has a total of 45 points, securing the second-highest position in the USA.
- PacificCrest accumulates 9 points, ranking as the third-highest winery in the USA
Output table is ordered by country in ascending order.
Solution
Approach: Aggregate, rank, then pivot with LEFT JOINs
A winery can appear in several rows of a country (RoyalVines in the USA has 47 and 39), so first sum the points per country and winery. Then number the wineries inside each country by total points descending and winery name ascending, and build the label name (total).
Finally take rank 1 as the base row and LEFT JOIN the same ranked set for rank 2 and rank 3. A country with fewer than three wineries gets NULL there, which IFNULL turns into the required placeholder text. ROW_NUMBER is enough because the winery name breaks every tie.
WITH ranked AS (
SELECT country,
CONCAT(winery, ' (', total, ')') AS label,
ROW_NUMBER() OVER (PARTITION BY country ORDER BY total DESC, winery) AS rk
FROM (
SELECT country, winery, SUM(points) AS total
FROM Wineries
GROUP BY country, winery
) AS w
)
SELECT r1.country,
r1.label AS top_winery,
IFNULL(r2.label, 'No second winery') AS second_winery,
IFNULL(r3.label, 'No third winery') AS third_winery
FROM ranked AS r1
LEFT JOIN ranked AS r2 ON r2.country = r1.country AND r2.rk = 2
LEFT JOIN ranked AS r3 ON r3.country = r1.country AND r3.rk = 3
WHERE r1.rk = 1
ORDER BY r1.country;