Эта часть немного избыточна:
SELECT d.country, d.Year, count(*) as number
FROM(
SELECT a.country, b.launched AS Year
FROM Classes a
INNER JOIN ships b ON a.class=b.class
) d
GROUP BY d.country, d.Year
Вы можете просто сделать это:
SELECT a.country, b.launched AS Year, count(*) as Number
FROM Classes a
INNER JOIN ships b ON a.class=b.class
GROUP BY a.country, b.launched
Затем вы можете использовать ROW_NUMBER
, чтобы назначить строку с наибольшим числом встрана a 1
:
SELECT a.country, b.launched AS Year, count(*) as Number,
ROW_NUMBER() over (PARTITION BY a.country ORDER BY COUNT(*) DESC) as RN
FROM Classes a
INNER JOIN ships b ON a.class=b.class
GROUP BY a.country, b.launched
Затем просто сделайте внешний выбор, чтобы получить строки, где RN = 1
:
SELECT country, year, number
FROM (SELECT a.country, b.launched AS Year, count(*) as Number,
ROW_NUMBER() over (PARTITION BY a.country ORDER BY COUNT(*) DESC) as RN
FROM Classes a
INNER JOIN ships b ON a.class=b.class
GROUP BY a.country, b.launched
) A
WHERE RN = 1