1
Good morning, I’m conducting a course and I’m stuck on an exercise.
In the exercise I have to return the maximum popularity value of each musical genre by country, so far all well, I managed to perform the procedure, the problem is that in the exercise has country that the maximum popularity value repeats, that is, has country that can have two musical generos of equal popularity.
I would like to know how to display the two generous, given that when I put GROUP BY it groups everything and displays only one country, and if there is another way to do this without sub-query
SELECT MAX(total) total, pais, genero, id_genero
FROM(SELECT Count(*) total, BillingCountry pais, g.Name genero, g.GenreId id_genero
FROM Genre g
JOIN Track t
ON g.GenreId = t.GenreId
JOIN InvoiceLine il
ON t.TrackId = il.TrackId
JOIN Invoice i
ON il.InvoiceId = i.InvoiceId
GROUP BY 2, 3
ORDER BY 2, 1 DESC)t1
GROUP BY 2
I could ask the question your data model and if possible a 2 lines with data to understand better?
– Ricardo Pontual
In thin, you are setando a group by 2, technically, depending on the case, you will always have the return of a column even, you took a look at that? if it’s something from the sgbd syntax you’re using, ignore the comment then ...
– Philip Developer
Ricardo, I put the data model and the result I have to get
– JacksonMauricio
Tip , use a subselect with MAX ( https://forum.imasters.com.br/topic/512214-selected-item-max/? do=findComment&comment=2030107 )
– Motta
Motta, Voce tells me to make another query in select that already has MAX?
– JacksonMauricio
Motta, got it, thanks for your help
– JacksonMauricio