I’m having a table with an user-ID and the Webside-IDs he navigated through. The table looks like this:
| user-ID | website-ID |
| 1 | 1 |
| 1 | 2 |
| 1 | 1 |
| 1 | 4 |
| 2 | 1 |
| 2 | 2 |
| 2 | 5 |
| 2 | 2 |
I’d like to select the website-ID each user has been into the most. As you can see, a min, max or avg won’t work here. Any ideas?
This may look complicated but it will only aggregate the data once only, then pass through it ranking the results and choosing only the first one
The inner select generates counts for each user-website combination and orders it by the most visits. This is then passed through the middle query that ranks the records
per user, giving the rank in the columnr.This is a variation, which will show ALL websites for a user that have EQUAL rank. The difference with the previous query is that if websites A and B both have 10 visits from user X, both A and B are listed in the result whereas the previous query selects one randomly to show.
This is the test table used