[Homework] Database Basics - Table Relations - Problem18. Continents and Currencies *
Някой може ли да ми каже защо този код не работи?
SELECT
ContinentCode,
MAX(CurrencyCode) AS CurrencyCode,
MAX(CurrencyUs) AS CurrencyUsage
FROM (SELECT c.ContinentCode,
cr.CurrencyCode,
COUNT(cr.Description) AS CurrencyUs
FROM Currencies cr
JOIN Countries c ON cr.CurrencyCode = c.CurrencyCode
GROUP BY c.ContinentCode, cr.CurrencyCode
HAVING COUNT(cr.Description) > 1) as sel
GROUP BY ContinentCode
ORDER BY ContinentCode
Изкарва ми този резултат, а би трябвало да има още един ред - AS - AUD - 2. Тъй като двете стойности са равни, не би ли трябвало да ми изкара и този ред?
ContinentCode CurrencyCode CurrencyUsage
AF XOF 8
AS ILS 2
EU NOK 26
NA XCD 8
OC XPF 8
Да, така стана. Мерси!
SELECT
sel.ContinentCode,
sel.CurrencyCode AS CurrencyCode,
sel.CurrencyUs AS CurrencyUsage
FROM (SELECT c.ContinentCode,
cr.CurrencyCode,
COUNT(cr.Description) AS CurrencyUs,
DENSE_RANK() over (partition by c.ContinentCode order by COUNT(cr.CurrencyCode) desc) as rankk
FROM Currencies cr
JOIN Countries c ON cr.CurrencyCode = c.CurrencyCode
GROUP BY c.ContinentCode, cr.CurrencyCode
HAVING COUNT(cr.Description) > 1) as sel
where sel.rankk = 1
ORDER BY ContinentCode