i am using SQL Server, considering the following table:
Content Table
title | topic | id_user |
---|---|---|
Title 1 | Topic1 | 1 |
Title 2 | Topic1 | 1 |
Title 3 | Topic2 | 2 |
Title 4 | Topic1 | 3 |
Title 5 | Topic1 | 1 |
Title 6 | Topic1 | 3 |
Title 7 | Topic2 | 2 |
When I run I get the following:
SELECT topic, id_user, COUNT(*) AS total
FROM Content
GROUP BY topic, id_user
ORDER BY total DESC
Result
topic | id_user | total |
---|---|---|
Topic1 | 1 | 3 |
Topic1 | 3 | 2 |
Topic2 | 2 | 2 |
In this case there are two users (there could be more) with Topic1, I only want to show the one with the highest total count value of their respective Topic
For example get the following result:
Expected Result
topic | id_user | total |
---|---|---|
Topic1 | 1 | 3 |
Topic2 | 2 | 2 |
Any clue how this could be done?