I need the TOP 1 string from 2 columns, the value should be returned ordered by number of appearances and by name descending. This is in SQL-Server.
Here’s an example table:
------------------------------
| Column1 | Column2 |
------------------------------
| John | Michael |
------------------------------
| Thomas | John |
------------------------------
| Martin | Rick |
------------------------------
| David | Thomas |
------------------------------
| John | Martin |
------------------------------
What the resulting order should look like:
----------------------------
| Column1 | COUNT |
----------------------------
| John | 3 |
----------------------------
| Martin | 2 |
----------------------------
| Thomas | 2 |
----------------------------
| David | 1 |
----------------------------
| Michael | 1 |
----------------------------
| Rick | 1 |
----------------------------
What the result should look like:
----------------------------
| Column1 | COUNT |
----------------------------
| John | 3 |
----------------------------
To get most used name and choose alphabetically first among them:
If you want to do the same but with one column only: