Retrieving only max values through SQL join table

Jernej K :

I am trying to use an inner join to retrieve the best ratings for certain categories with the following SQL statement:

select expertise.category, sk.type from expertise inner join 
  (select skills.expertise_uuid, skills.type, max(skills.rating) as rating 
  from skills grouped by skills.expertise_uuid) as sk 
on sk.expertise_uuid = expertise.uuid);

I have the following two tables:

mysql> select * from expertise;
+--------------------------------------+-----------------------+----------+--------------------------------------+
| uuid                                 | category              | favorite | user_profile_uuid                    |
+--------------------------------------+-----------------------+----------+--------------------------------------+
| 2a325545-5713-4b56-b3a1-9655875e2da7 | Frameworks & Tools    |        0 | 32251c91-aff3-4aa0-8350-8a556db9bcd5 |
| 6166a5c4-2b64-4658-896a-c99fc79fa7ce | Programming languages |        1 | 32251c91-aff3-4aa0-8350-8a556db9bcd5 |
| 91d8b6da-57f5-49ee-83fc-37b8ac430e49 | Databases             |        0 | 32251c91-aff3-4aa0-8350-8a556db9bcd5 |
| b452ffc0-a920-4b5c-8e96-947e5c7e1a31 | OS & Software         |        0 | 32251c91-aff3-4aa0-8350-8a556db9bcd5 |
| c338733f-3a58-4e90-978c-1977abf6001c | Application servers   |        0 | 32251c91-aff3-4aa0-8350-8a556db9bcd5 |
| e0149f2b-5bb4-4f27-aa37-58b228409bec | Languages             |        0 | 32251c91-aff3-4aa0-8350-8a556db9bcd5 |
| eac89c26-f204-4b25-9b7e-3cb66cc75962 | Test                  |        0 | 32251c91-aff3-4aa0-8350-8a556db9bcd5 |
+--------------------------------------+-----------------------+----------+--------------------------------------+
7 rows in set (0.00 sec)
mysql> select * from skills;
+--------------------------------------+--------+-------+------------+--------------------------------------+
| uuid                                 | rating | scale | type       | expertise_uuid                       |
+--------------------------------------+--------+-------+------------+--------------------------------------+
| 130580c4-9359-4486-9c73-1f6daeac183a |      6 |    10 | Linux      | b452ffc0-a920-4b5c-8e96-947e5c7e1a31 |
| 17842ee5-c0c3-412f-8b6a-eff8a9f72f95 |      3 |    10 | C++        | 6166a5c4-2b64-4658-896a-c99fc79fa7ce |
| 1967f7ab-e039-466c-af4d-deaa1b2dc5fe |      6 |    10 | Subversion | 2a325545-5713-4b56-b3a1-9655875e2da7 |
| 1bac44e7-75c5-45a9-b11d-fa74727e68a4 |      7 |    10 | MySQL      | 91d8b6da-57f5-49ee-83fc-37b8ac430e49 |
| 1f2bc3e9-83f8-44a7-aa6a-65bb7eebb279 |      6 |    10 | Android    | 6166a5c4-2b64-4658-896a-c99fc79fa7ce |
| 26e542fb-5a3a-46fd-847f-d69cc09608fb |      8 |    10 | Java       | 6166a5c4-2b64-4658-896a-c99fc79fa7ce |
| 28572313-a9f8-41d1-8a62-a2535df23787 |      5 |    10 | WebSphere  | c338733f-3a58-4e90-978c-1977abf6001c |
| 55f30986-4b61-4347-aaca-2346799ccd42 |     10 |    10 | Slovene    | e0149f2b-5bb4-4f27-aa37-58b228409bec |
| 5dad7632-6bbf-4b70-801b-d24e2f24560e |      7 |    10 | Eclipse    | b452ffc0-a920-4b5c-8e96-947e5c7e1a31 |
| 676dc2b0-de01-47ef-8c10-be81a57d3a57 |      6 |    10 | JavaScript | 6166a5c4-2b64-4658-896a-c99fc79fa7ce |
| 8db56cf5-e00a-4652-8f08-39a6ba9961ed |      3 |    10 | Drinking   | NULL                                 |
| a184ee09-05ab-4228-ba5d-0019d578367d |      7 |    10 | Git        | 2a325545-5713-4b56-b3a1-9655875e2da7 |
| a22331ed-ce03-4dd9-a541-23cdccb0bde3 |      8 |    10 | Windows    | b452ffc0-a920-4b5c-8e96-947e5c7e1a31 |
| b32c8898-117f-4685-b988-0d6012d2fdaa |      2 |    10 | C#         | 6166a5c4-2b64-4658-896a-c99fc79fa7ce |
| b93c9c13-68c3-4f95-88e0-3ae3609b36be |      8 |    10 | IntelliJ   | b452ffc0-a920-4b5c-8e96-947e5c7e1a31 |
| be2a85aa-a093-441c-adf5-3c9415d6112f |      7 |    10 | MacOS      | b452ffc0-a920-4b5c-8e96-947e5c7e1a31 |
| d5844c34-fa9a-45cc-913d-9fb82d408d7f |      7 |    10 | English    | e0149f2b-5bb4-4f27-aa37-58b228409bec |
| d72e50c4-0fbc-415b-970e-d33c8386174d |      2 |    10 | Scala      | 6166a5c4-2b64-4658-896a-c99fc79fa7ce |
| dfefcc91-194e-4b54-ab07-51e186f60c18 |      5 |    10 | PHP        | 6166a5c4-2b64-4658-896a-c99fc79fa7ce |
| ee2d4b52-0b22-4d1c-a0b1-27a0d23ccbca |      6 |    10 | Oracle     | 91d8b6da-57f5-49ee-83fc-37b8ac430e49 |
| fd936710-edff-4cdb-8924-ff513f0d2315 |      7 |    10 | Jetty      | c338733f-3a58-4e90-978c-1977abf6001c |
+--------------------------------------+--------+-------+------------+--------------------------------------+
21 rows in set (0.00 sec)

How would I be able to retrieve the category from expertise and the corresponding type in skills for the highest rated skills (rating column)? The result should look like this:

+-----------------------+--------+--------+
| category              | rating |  type  |
+-----------------------+--------+--------+
| Frameworks & Tools    |      7 |   Git  |
| Programming languages |      8 |  Java  |
| Databases             |      7 | MySQL  |
| OS & Software         |      8 | Eclipse|
| Application servers   |      7 | Jetty  |
| Languages             |     10 | Slovene|
+-----------------------+--------+--------+

I looked up several stack overflow questions, but I did not find any answer that could be used in this example.
Gordon Linoff :

If I understand correctly, you can use row_number():

select es.*
from (select e.category, s.rating, s.type,
             row_number() over (partition by e.category order by s.rating desc) as seqnum
      from expertise e join
           skills s
           on e.uuid = s.expertise_uuid
    ) es
where seqnum = 1;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=298030&siteId=1